Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere should calculation logic go in a Rails app?
    primarykey
    data
    text
    <p>I have an app that models a House. The House has_many Rooms, Rooms has_many Lights and Small_appliances, etc. I also have a controller called Calculator that is how the app is accessed. Data is added to the house (and its rooms) using the Calculator controller. Then a report is generated, which is located at app/views/calculator/report.html.erb.</p> <p>My question is where should all the calculations and logic for the report go? Currently I have it all in the view, with some things in calculator_helper. Normally this would go in the model, right? But Calculator doesn't have a model that was generated. What is the standard for this?</p> <p>Here is the calculator controller.</p> <pre><code>class CalculatorController &lt; ApplicationController def index end def save_house @house = House.new(params[:house]) respond_to do |format| if @house.save format.html { render :action =&gt; 'add_rooms', :id =&gt; @house } format.xml { render :xml =&gt; @house, :status =&gt; :created, :location =&gt; @house } else format.html { render :action =&gt; 'index' } format.xml { render :xml =&gt; @house.errors, :status =&gt; :unprocessable_entity } end end end def add_rooms @house = House.find(params[:id]) @rooms = Room.find_by_house_id(@house.id) rescue ActiveRecord::RecordNotFound logger.error("Attempt to access invalid house #{params[:id]}") flash[:notice] = "You must create a house before adding rooms" redirect_to :action =&gt; 'index' end def add_room @room = Room.new(params[:room]) @house = @room.house respond_to do |format| if @room.save flash[:notice] = "Room \"#{@room.name}\" was successfully added." format.html { render :action =&gt; 'add_rooms' } format.xml { render :xml =&gt; @room, :status =&gt; :created, :location =&gt; @room } else format.html { render :action =&gt; 'add_rooms' } format.xml { render :xml =&gt; @room.errors, :status =&gt; :unprocessable_entity } end end rescue ActiveRecord::RecordNotFound logger.error("Attempt to access invalid house #{params[:id]}") flash[:notice] = "You must create a house before adding a room" redirect_to :action =&gt; 'index' end def report flash[:notice] = nil @house = House.find(params[:id]) @rooms = Room.find_by_house_id(@house.id) rescue ActiveRecord::RecordNotFound logger.error("Attempt to access invalid house #{params[:id]}") flash[:notice] = "You must create a house before generating a report" redirect_to :action =&gt; 'index' end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload