Ruby on Rails Tuesday, June 26, 2012

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 ..}"

On Tue, Jun 26, 2012 at 1:58 PM, Timen E. <lists@ruby-forum.com> wrote:
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)) 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
 def create
   @complaint = Complaint.find(params[:complaint_id])
   @comment = @complaint.comments.create(params[:form])
   respond_to do |format|
     if @comment.save
     else
     flash[:notice] = "Fail!"
     end
   redirect_to complaint_path(@complaint)
   end
 end


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

<%= form_for([@complaints, @complaints.comments.build]) do |f| %>
 <div class="field">
   <%= f.label :name %><br />
   <%= f.text_field :name %>
 </div>
 <div class="field">
   <%= f.label :comment %><br />
   <%= f.text_area :comment %>
 </div>
 <div class="actions">
   <%= f.submit %>
 </div>
<% end %>


Thanks in advance

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.




--
Thanks, 
Aash

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment