I have a simple form view using a Rails 3.1.x application:
<%= form_for(:mymodel) do |f| %>
<% if @mymodel.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@mymodel.errors.count, "error") %> prohibited this model from being saved:</h2>
<ul>
<% @mymodel.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
Amount:
<%= f.text_field :count %><br /><br />
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
When submit is hit here, it posts to /mymodels/new. How do I force it to go to the right create action for mymodels_controller.rb?
Option 1: Follow the conventions and supply a new, unsaved record to the
form_forhelper. You probably already have such object on@mymodel, set on yournewaction of your controller. So the following snippet should work nicely:If it does not work, you can set
@mymodelon your action like this:Option 2: Be explicit about your URL and method: