开发者

Rails 3 - Unable to create a new post while at the SHOW view of another Post

I have a model named "Post". I want to use a modal form to create a new post while at the SHOW view of another post. Meaning while I am viewing the post named "John开发者_StackOverflow社区" in its show view, I would like to be able to create a new post from right there.

The problem I have is that the ID of the new post remains the same as the post I am viewing, and causes the update action to be fired instead of the create action. Any suggestions on how to handle this?


Build a new post with Post.new and use that in a form_for:

<%= form_for Post.new %>
  <%= render "form" %>
<% end %>

Of course this means you'll need to remove the form_for from your form partial if you have it in there, but that's a small sacrifice to make.

However if you really don't want to do that then you will have to pass through a local variable to the form partial to indicate which post you want to display. On the show page you'd have this:

<%= render :partial => "form", :locals => { :post => Post.new } %>

In the new and edit views you'd do this:

<%= render :partial => "form", :locals => { :post => @post } %>

The line is a little bit longer, but that would allow you to keep the form_for tag inside the form partial and not clog up the three other views with it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜