Tutorial :Rails 2 - partials: what does @comment = Comment.new mean?



Question:

I am working through a tutorial with the following code:

<h3>New Comment</h3>     <%= render :partial => @comment = Comment.new,     :locals => { :button_name => "Create" } %>  

I believe that 'render :partial => @comment' works like 'render :partial => "comment", :object => @comment'

Where does ' = Comment.new' fit in? Is it shorthand for :object?

Alan


Solution:1

In Ruby terms,

@obj = Object.new # returns @obj  

So you're rendering a comment partial and creating a new comment object that it can work with at the same time.


Solution:2

See http://apidock.com/rails/ActionView/Partials section "Rendering objects with the RecordIdentifier":

# <%= render :partial => "accounts/account", :locals => { :account => @buyer } %>  <%= render :partial => @account %>  

Though documented, this is hardly used. The new+assignation (as explained by aharon) works, but it seems a bit tricky. In a tutorial you would expect to find a more orthodox approach:

  • Create objects in controllers not in views.
  • Use render :partial => 'mypartial', :locals => {...}

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »