Flip
Flip

Reputation: 6761

Rails: How to properly prepare variables for view?

Is there a best practice for preparing variables for a view in Rails? Basically I want to know if, taking for example a hash like @campaign, I should extract the needed values (e.g. list_id) in the view or in the controller?

@campaign = {
             "id"=>"8a9asd64b94", 
             "type"=>"regular", 
             "create_time"=>"2013-04-17T12:07:58+00:00", 
             "recipients"=> {
                             "list_id"=>"aecadsasd0b5", 
                             "segment_text"=>"ljsadlkjasd"
                            }
             }

Upvotes: 0

Views: 75

Answers (2)

tanvir2362
tanvir2362

Reputation: 76

Ideally, you should do the queries in the controller and pass that to the view. So in your case, @campaign should be in the controller, and you can use @campaign["recipients"]["list_id"] in the view.

Upvotes: 0

Bart Jedrocha
Bart Jedrocha

Reputation: 11570

You typically send the entire object down to the view and access the individual properties there. For example

# in controller

@post = Post.find(params[:id])

# in view

<h1><%= @post.title %></h1>
<p><%= @post.body %></p>

In your example, you should send the entire @campaign object down to the view and access the individual components there.

Upvotes: 2

Related Questions