Reputation: 1
I keep getting Post expected, got String error. If someone can help me thanks in advance.
<% form_for :comment, :url=>{:controller=>"comments", :action=>"create"} do |f|%>
<p>
<%= f.label :body, "New Comment"%><br />
<%= f.text_area :body %>
<%= f.hidden_field :post, :value=>@post.id %>
</p>
<p><%= f.submit "Add Comment"%></p>
<% end%>
def create
@comment = Comment.create(params[:comment])
if @comment.save
redirect_to(:controller=>"posts" ,:action=>'index')
else
redirect_to(:controller=>"posts" ,:action=>'show', :id=>"post.id")
end
end
Upvotes: 0
Views: 807
Reputation: 51707
Your second redirect should be:
redirect_to(:controller=>"posts" ,:action=>'show', :id=> @comment.post.id)
Although, looking at this, you could definitely use some better patterns to clean things up. If you are using RESTful routes, I would change your create action to be:
def create
@post = params[:id]
@comment = @post.comments.build(params[:comment])
if @comment.save
redirect_to posts_url
else
redirect_to post_url(@post)
end
end
This would allow you to remove the hidden field in your form since it should be getting passed through the URL as the ID.
Upvotes: 1
Reputation: 4455
First of all, shouldn't you change post.id
to @post.id
(and maybe create a post object)?
Upvotes: 0