# Adding comments

**URL:** <https://discuss.rubyonrails.org/t/adding-comments/64302>\
**Category:** rubyonrails-talk\
**Created:** [June 26, 2012, 8:28am UTC](https://discuss.rubyonrails.org/t/adding-comments/64302 "2012-06-26T08:28:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 26, 2012, 8:28am UTC](https://discuss.rubyonrails.org/t/adding-comments/64302/1 "2012-06-26T08:28:45Z")

</div>

Heya,

I've been toying around a little more with rails and getting the hang of it. Nonetheless the, most likely, easiest thing for you i can't achieve somehow. I'm trying to add some comments to my post(complaint)(Same way the starter tutorial does it([http://guides.rubyonrails.org/getting\_started.htm](http://guides.rubyonrails.org/getting_started.htm))) but i can't seem to get it to work. It goes to complaints/3/comments and gives me a blank page. There is a record being created in my database, but except for the complaint\_id it's empty and the create function doesn't redirect me back to the complaint it should be redirecting to. Could you guys take a look at my code?

The create function in comments\_controller.rb &nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@complaint = Complaint.find(params[:complaint\_id]) &nbsp;&nbsp;&nbsp;&nbsp;@comment = @complaint.comments.create(params[:form]) &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @comment.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "Fail!" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to complaint\_path(@complaint) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

The form in comments/\_form.html.erb which is rendered in my complaints/show view

\<%= form\_for([@complaints, @complaints.comments.build]) do |f| %\> &nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :name %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :name %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :comment %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_area :comment %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="actions"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.submit %\> &nbsp;&nbsp;\</div\> \<% end %\>

Thanks in advance

---

<div class="post-metadata">

**Author:** ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.rubyonrails.org/u/aash_dhariya)\
**Post date:** [June 26, 2012, 9:10am UTC](https://discuss.rubyonrails.org/t/adding-comments/64302/2 "2012-06-26T09:10:00Z")

</div>

Try this:

controller:

def create @complaint = Complaint.find(params[:complaint\_id]) @comment = @complaint.comments.create(params[:comment]) respond\_to do |format| unless @comment.save

```
   flash[:notice] = "Fail!"
 end
 format.html { redirect_to complaint_path(@complaint) }

```

end end

check the params array. While creating the comments, pass the params array appropriately. I think may be that’s where the problem lies. As for redirect to another page, you need to write your redirect statements in “format.html { redirect\_to …}”

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 27, 2012, 8:27am UTC](https://discuss.rubyonrails.org/t/adding-comments/64302/3 "2012-06-27T08:27:05Z")

</div>

Have a look at the Rails Guide on Debugging, that will show you techniques that you can use to debug the code and work out what is going on.

Colin
