开发者

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

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 => @c开发者_如何转开发omment'

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

Alan


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.


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 => {...}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜