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” 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.newand use that in aform_for:Of course this means you’ll need to remove the
form_forfrom 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
showpage you’d have this:In the
newandeditviews you’d do this:The line is a little bit longer, but that would allow you to keep the
form_fortag inside the form partial and not clog up the three other views with it.