Re: Routing acts_as_commentable form_for submission

Neil Cauldwell
2008-03-18T20:16:44Z

Adam Cohen wrote:
> oh, that''s just a convention that I use in partials to give the
passed
> in
> object a more descriptive name.  In this case, however,
''items'' isn''t
> all
> that more descriptive! :)
> In any case, the code I wrote is supposed to be called using the
> following
> form:
>
> <%= render(:partial => ''views/shared/comments'',
:object => @product %>
>
> and then in the ''views/shared/_comments.rhtml'' file, the
@product object
> that''s passed in will be accessible by the
''comments'' local variable
> (remember in partials that a local variable with the same name as the
> partial file is automatically created for you which allows you to access
> the
> passed in object).
>
> So you could either use
>
> <% for comment in comments.comments %> ...
>
> which to me looks a bit confusing, or you could use
>
>
> <% items = comments %>
> <% for comment in item.comments %>
>
> and of course if you wanted to be more descriptive, you could use
>
> <% commentable_item = comments %>
>
> which is easier to read.
>
> Adam
>
>
>
> On Tue, Mar 18, 2008 at 5:23 AM, Neil Cauldwell <

Thanks Adam. It works, and I''ll admit to being completely baffled by
the
<% item = comments %> at first. But it made sense when you explained
that the partial creates a local variable after it''s own naming
convention (which in my case meant @news_item = comments = item ==
confusion). However, I opted for this;

My partial call does this;

<%= render :partial => "comments/comments", :object =>
@news_item %>


And my _comments.html.erb looks like this;

<% for comment in object.comments %>

<%=h comment.user.login %>
<%= comment.comment %>
Posted <%= time_ago_in_words(comment.created_at) %>
ago

<% end %>

Comment on this <%= object.class %>
<% form_tag(comments_path) do -%>
  <%= hidden_field_tag ''comment[user_id]'', current_user.id
%>
  <%= hidden_field_tag ''comment[commentable_id]'', object.id
%>
  <%= hidden_field_tag ''comment[commentable_type]'',
object.class %>
  <%= text_area_tag ''comment[comment]'', nil, :style =>
''width:100%;
height:100px;'' %>
  <%= submit_tag ''Submit'' %>
<% end %>

It seems to be working just fine. Please let me know if I''m wrong to
rely on ''object'' as the means with which to access the :object
=>
@news_item. It seems to make sense, though.