[英]ArgumentError wrong number of arguments (1 for 3..4)
I get an ArgumentError for line #3 in my views/-/new.html.erb file that states: 在我的views /-/ new.html.erb文件中, 第3行出现ArgumentError:
"wrong number of arguments (1 for 3..4)" “参数数量错误(1代表3..4)”
<div class='form-group'>
<%= form.label :category %>
<%= form.select "category", options_from_collection_for_select([{1 => 'Food'}, {2 => 'Entertainment'}]) %>
</div>
The Application trace states: 应用程序跟踪状态:
app/views/events/new.html.erb:14:in block in _app_views_events_new_html_erb__1569841425540097418_70204987081640' app/views/events/new.html.erb:5:in
_app_views_events_new_html_erb__1569841425540097418_70204987081640' app / views / events / new.html.erb:14:在
block in _app_views_events_new_html_erb__1569841425540097418_70204987081640' app/views/events/new.html.erb:5:in
_app_views_events_new_html_erb__1569841425540097440'
erb:14 is line #3 above, and erb:5 is erb:14是上方的第3行 ,而erb:5是
<%= form_for @event do |form| %>
The error message is being thrown by options_from_collection_for_select([{1 => 'Food'}, {2 => 'Entertainment'}])
由
options_from_collection_for_select([{1 => 'Food'}, {2 => 'Entertainment'}])
引发错误消息
[{1 => 'Food'}, {2 => 'Entertainment'}]
is an array being passed as one argument to options_from_collection_for_select
method; [{1 => 'Food'}, {2 => 'Entertainment'}]
是作为一个参数传递给options_from_collection_for_select
方法的数组; hence the error message. 因此错误消息。
The correct form for calling the options_from_collection_for_select
helper method is 调用
options_from_collection_for_select
辅助方法的正确形式是
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
See more details and usage examples at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select 在http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select中查看更多详细信息和用法示例
I think you'd be better off using options_for_select
, which will work with the arguments you've provided and just replace the options_from_collection_for_select
in your code. 我认为您最好使用
options_for_select
,它将与您提供的参数一起使用,而只需替换代码中的options_from_collection_for_select
。 The documentation for which can be found here . 可在此处找到其文档。
You need to decide whether you want the number (1, 2 etc.) or the word (Food, Entertainment etc.) to be the "value" which reaches your back-end. 您需要确定是否要将数字(1、2等)或单词(食品,娱乐等)用作到达后端的“值”。 For example:
例如:
options_for_select({'Food' => 1, 'Entertainment' => 2})
The output of the above in HTML would be as follows: 以上内容在HTML中的输出如下:
<option value="1">Food</option>
<option value="2">Entertainment</option>
The options_from_collection_for_select method requires 3 arguments. options_from_collection_for_select方法需要3个参数。 You've only provided one:
[{1 => 'Food'}, {2 => 'Entertainment'}]
. 您只提供了一个:
[{1 => 'Food'}, {2 => 'Entertainment'}]
。 Your second argument should be a method/attribute on each object in the collection which represents the "value", the third should be the label for that value. 第二个参数应该是表示“值”的集合中每个对象的方法/属性,第三个参数应该是该值的标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.