Re: Routing acts_as_commentable form_for submission

Adam Cohen
2008-03-18T18:54:20Z

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 <
rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:

>
> Adam Cohen wrote:
> > you could create a CommentsController and use hidden form fields in a
> > shared
> > view to tell the comments controller which type of object the comment
> > should
> > be associated with.
> > For example:
> >
> > app/views/shared/_comments.rhtml
> >
> > <% item = comments %>
> >
> > <% for comment in item.comments %>
> > Comment posted by <%= comment.user.full_name %>
> > <%= comment.body %>
> > <% end %>
> >
> > Comment on this <%= item.class %>
> > <% form_tag(comments_path) do -%>
> >   <%= hidden_field_tag ''comment[commentable_id]'',
item.id %>
> >   <%= hidden_field_tag
''comment[commentable_type]'', item.class %>
> >   <%= text_area_tag ''comment[body]'', nil, :style
=> ''width:100%;
> > height:100px;'' %>
> >   <%= image_submit_tag
(''post_comment_button.png'') %>
> > <% end %>
> >
> > The above would work with the polymorphic Comment class used by
> > acts_as_commentable
> >
> > Adam
> >
> >
> >
> >
> > On Mon, Mar 17, 2008 at 3:48 PM, Neil Cauldwell <
>
> Thanks Adam, that''s pretty much what I was hoping I could do, i.e.
> keeping a generic comments view which can handle object.comments, rather
> than having to define news_item.comments, user.comments etc.
>
> However, one thing has confused me;
>
> <% item = comments %>
>
> Why would I want to define item as the comments?
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >