form_for wrong number of arguments (3 for 2) since upgrade to rails 3.1

萝らか妹 提交于 2020-01-02 01:03:33

问题


this form_for used to work before I ported my application to rails 3.1

<div class="form-box" style="padding-left:1em;">
  <%
     action = @existing_mass.nil? ? "add_to_power_plant": "update_power_plant_substrate";
     submit_button_label = @existing_mass.nil? ? 'Add': 'Update';
  %>

  <%= form_for :substrate_mass, @substrate_mass, :remote => true, :url => { :action => action, :substrate_id => @substrate_mass.substrate  } do |f| %>
    <div>
      <%= f.label :quantity_per_year, "Quantity" %>
      <%= f.text_field :quantity_per_year, :size => 5, :onclick => 'this.select();', :value => @substrate_mass.quantity_per_year %>
    </div>

    <div class="actions" style="float:right;">
      <%= f.submit submit_button_label %>
    </div>
    <br/> 
  <% end %>
</div>

I have spent over 4 hours trying to figure out what's wrong ... there is definitely something I am not understanding anymore

I get the error:

wrong number of arguments (3 for 2)

Note that I am trying to update an variable that is not an activerecord object. It's just an object that is not stored in the database.

Hope someone can help.

cheers


回答1:


form_for only takes two arguments, the record, and options, although record may be several things, including a simple symbol, an object, or an array.

Try just dropping the first symbol and sending your object. If you model does not include ActiveModel::Naming, you may set the name via the :as option.

<%= form_for @substrate_mass, :as => 'substrate_mass', ... %>

More help may be found here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

Or to view the source directly:
https://github.com/rails/rails/blob/v3.1.0/actionpack/lib/action_view/helpers/form_helper.rb#L353



来源:https://stackoverflow.com/questions/7523252/form-for-wrong-number-of-arguments-3-for-2-since-upgrade-to-rails-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!