# Routing acts\_as\_commentable form\_for submission

**URL:** <https://www.ruby-forum.com/t/routing-acts-as-commentable-form-for-submission/132197>\
**Category:** Rails\
**Created:** [March 17, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/routing-acts-as-commentable-form-for-submission/132197 "2008-03-17T20:48:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 17, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/routing-acts-as-commentable-form-for-submission/132197/1 "2008-03-17T20:48:31Z")

</div>

I have a Group model and a NewsItem model, and NewsItems are nested in  
Groups.

I’m trying to add acts\_as\_commentable powered comments to the NewsItem  
model, but I haven’t worked out how to handle the submission of new  
comments when ‘comments’ aren’t a resource in the URL. I don’t have a  
CommentsController or a comments route (I was under the impression that  
they aren’t necessary). I do have a ‘comments/\_form.html.erb’ which I  
was going to call from the ‘news\_items/show.html.erb’.

Should I put a ‘@new\_comment = Comment.new’ instance variable in the  
news\_item\_controller.rb ‘show’ action (or any other action that will  
receive new comments), and use a form\_for helper on the @new\_comment in  
the view? Because that the method I was expecting to take, but I just  
realised I don’t know how to handle ‘form\_for’ url for the @new\_comment  
when the comment is being created within the group\_news\_item\_path, i.e;

\<% form\_for(@new\_comment) do |f| %\> # where do I point this?  
\<%= render :partial =\> ‘comments/form’, :locals =\> { :f =\> f }%\>  
\<% end %\>

And I also realised that this approach could create a fair bit of  
duplication, if I’m going to maxmise this polymorphic plugin, and re-use  
the comment/\_form.html.erb across the ‘show’ actions for many other  
models - all my controller actions will need the Comment.new adding to  
them. Is there a better way or something I’m missing?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 18, 2008, 5:19am UTC](https://www.ruby-forum.com/t/routing-acts-as-commentable-form-for-submission/132197/2 "2008-03-18T05:19:20Z")

</div>

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 C. \<

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 18, 2008, 10:23am UTC](https://www.ruby-forum.com/t/routing-acts-as-commentable-form-for-submission/132197/3 "2008-03-18T10:23:00Z")

</div>

Adam C. 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 C. \<

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?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 18, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/routing-acts-as-commentable-form-for-submission/132197/4 "2008-03-18T19:55:45Z")

</div>

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 C. \<

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 18, 2008, 9:16pm UTC](https://www.ruby-forum.com/t/routing-acts-as-commentable-form-for-submission/132197/5 "2008-03-18T21:16:44Z")

</div>

Adam C. 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 C. \<

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.
