Does Rails form send to / new instead of / create?

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 a message is sent here, it is sent to /mymodels/new. How to get her to go to the correct create action for mymodels_controller.rb?

+3
source share
1 answer

Option 1: Follow the conventions and put the new unsaved record in the assistant form_for. You probably already have such an object @mymodelinstalled on newyour controller in your action . So the following snippet should work beautifully:

<%= form_for(@mymodel) do |f| %>

If this does not work, you can set it @mymodelto your action as follows:

class MyModelsController
  def new
    @mymodel = MyModel.new
  end
end

2: URL- :

<%= form_for(:mymodel, :url => create_mymodel_path, :method => :post) do |f| %>
+5

All Articles