views:

70

answers:

2

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

+6  A: 

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.

aharon
Thank you, I couldn't find any reference to this format via Google
Alan
+3  A: 

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 => {...}
tokland
Thanks for the reference to apidock.com, it looks a very useful resource - Alan
Alan
I prefer the more verbose version myself too.
nathanvda