Web-development – Which is the convention in Rails to perform calculations and display the results

rubyruby-on-railsweb-applicationsweb-development

So I'm working on a personal project on Rails to learn more about this framework, and I wanted to add a feature in which the user choose a particular record from the view, and after some calculations are made (which involves an array of 80 integers) I show the result… And because I find it looks ugly in the way I did I just wondered what was the convention to do this kind of things…

  1. In detail what I do is show you a list of records to the user, the
    user selects a record and a number, and then click the "Calculate exp" button…

one

  1. Then in the controller I check if the parameters exist, if they exist then I define the array and do the calculations based on the input number, on the record attributes and the array (the array never changes and could be defined as a constant in fact) that I define in the controller, and at the end I assing the result to an instance variable.

enter image description here

  1. Finally the result is shown on the same page, only if the instance variable that hold the result and is defined on the controller (after checking that the parameters exists) exists.

So my question is if I'm doing it the right way (is my first time trying to do something "serious" with rails and I don't know if I'm doing it right, especially by declaring an array of 80 elements in the controller, I don't like how it looks and I don't feel it's right.).

Best Answer

This is what a conventional rails controller would look like:

class HeroesController < ApplicationController
  before_filter :authenticate_user!
  before_filter :user_only

  def index
    @heroes = current_user.heroes.find_by_level(params[:level])
  end
end

Controllers should map to resources, and your controller is returning a list of heroes. Hence, HeroesController#index. All the finding stuff goes in the model.