# Comment Form Problem

**URL:** <https://www.ruby-forum.com/t/comment-form-problem/111541>\
**Category:** Rails\
**Created:** [August 8, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/comment-form-problem/111541 "2007-08-08T12:49:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sashaski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sashaski](https://www.ruby-forum.com/u/sashaski)\
**Post date:** [August 8, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/comment-form-problem/111541/1 "2007-08-08T12:49:24Z")

</div>

Hi, I’ve recently implemented permalinks into my app and thus have had  
to change all “:id =\> @post” to use permalinks. However when I got to  
updating the comments form I hit a problem, the original code I had was:

\<%= form\_tag :action =\> ‘comment’, :id =\> @post %\>

So I thought I should change it to:

\<%= form\_tag :action =\> ‘comment’, :permalink =\> @post.permalink %\>

However when I test this by submitting a comment I receive the following  
error:

NameError in BlogController#comment

undefined local variable or method `post’ for  
#BlogController:0x3423d64

In my blog\_controller I have this for comments:

def comment  
Post.find\_by\_permalink(params[:permalink]).comments.create(params[:comment])  
flash[:notice] = “Added your comment.”  
redirect\_to :action =\> “show”, :permalink =\> post.permalink  
end

Any suggestions would be great!

---

<div class="post-metadata">

**Author:** ![sashaski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sashaski](https://www.ruby-forum.com/u/sashaski)\
**Post date:** [August 8, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/comment-form-problem/111541/2 "2007-08-08T14:41:39Z")

</div>

def comment  
Post.find\_by\_permalink(params[:permalink]).comments.create(params[:comment])  
flash[:notice] = “Added your comment.”  
redirect\_to :action =\> “show”, :permalink =\> post.permalink  
end

the post variable isn’t defined

```
redirect_to :action => "show", :permalink => post.permalink

```

maybe defining it should help:

```
post=Post.find_by_permalink(params[:permalink])
post.comments.create(params[:comment])
redirect_to :action => "show", :permalink => post.permalink
   # (or even)
redirect_to :action => "show", :permalink => params[:permalink]

```

no?

---

<div class="post-metadata">

**Author:** ![sashaski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sashaski](https://www.ruby-forum.com/u/sashaski)\
**Post date:** [August 16, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/comment-form-problem/111541/3 "2007-08-16T19:09:19Z")

</div>

No that didn’t work, although now I don’t get an undefined variable  
error. Instead it returns to the post page saying “Added your comment”,  
but there are no comments shown.

I looked inside the comments table in the database and found that the  
comments weren’t being added and that there was a column “post\_id”,  
however my post don’t have id’s anymore as they use permalinks so I need  
the comments to be associated to permalinks.

Any idea how to do that?
