Ok, to create an object in a view?

Is it possible to instantiate an object in the view before passing it in partial?

<%= render :partial => "trade_new", :locals => {:trade=>Trade.new("e", "b") } %>

      

Or is it better to instantiate any objects in the controller as instance variables:

@trade = Trade.new("e", "b")

      

and then pass the instance variable to the partial view as:

<%= render :partial => "trade_new", :locals => {:trade => @trade } %>

      

I guess it is better to create new objects in the controller to avoid duplication - for example, in the case where multiple templates might need to pass this new object in partial from the same activity.

0


source to share


1 answer


First, you can instantiate the object in the view. Nothing can explode in your face. However, you miss out on all the benefits of tiering the architecture.

Better to instantiate the object in the controller. Some of the reasons include: better reuse, much easier testing, better design because of decoupling.



See articles on presentation templates here .

+2


source







All Articles