It's maybe a little extra work but still an accepted pattern to have a separate form container for the new and edit actions, such as this:
new.html.erb
<%= form_for(@post, url: posts_path) do |f| %>
<%= render 'form', f: f %>
<%= f.submit 'Create' %>
<% end %>
edit.html.erb
<%= form_for(@post, url: post_path(@post)) do |f| %>
<%= render 'form', f: f %>
<%= f.submit 'Update' %>
<% end %>
So that's one option. You render the common form fields in the _form.html.erb
partial and do whatever's unique to your new/edit in their respective form containers.
But, if you'd really like to have just one form container file you can always do this:
<%= form_for(@post, url: (@post.new_record? ? posts_path : post_path(@post)) ) do |f| %>
Note: I'm not exactly sure how the locales gem works so I excluded that above... but if you do need to include the locale explicitly you can do that with e.g. post_path(@post, locale: params[:locale])
. Recall that post_path(@post)
is actually short-hand for post_path(id: @post)
. That is, the parameters you pass into the named route are ordered the same as your route expects them if not explicitly stated.