form_for自升级到rails 3.1以来,参数数目错误(3为2) [英] form_for wrong number of arguments (3 for 2) since upgrade to rails 3.1
问题描述
在将应用程序移植到Rails 3.1之前,此form_for可以正常工作
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>
我花了4个多小时试图找出问题所在...肯定是我不了解了什么
I have spent over 4 hours trying to figure out what's wrong ... there is definitely something I am not understanding anymore
我得到了错误:
错误的参数数量(3个代表2个)
wrong number of arguments (3 for 2)
请注意,我正在尝试更新不是activerecord对象的变量.它只是一个未存储在数据库中的对象.
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.
欢呼
推荐答案
form_for
仅采用两个参数,分别是record
和options
,尽管记录可能有很多东西,包括一个简单的符号,一个对象,或数组.
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.
尝试仅删除第一个符号并发送对象.如果您的模型不包含ActiveModel::Naming
,则可以通过:as
选项设置名称.
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', ... %>
可以在此处找到更多帮助:
http://api.rubyonrails.org/classes/ActionView /Helpers/FormHelper.html#method-i-form_for
More help may be found here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
或者直接查看源代码:
https://github.com /rails/rails/blob/v3.1.0/actionpack/lib/action_view/helpers/form_helper.rb#L353
Or to view the source directly:
https://github.com/rails/rails/blob/v3.1.0/actionpack/lib/action_view/helpers/form_helper.rb#L353
这篇关于form_for自升级到rails 3.1以来,参数数目错误(3为2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!