【问题标题】:ArgumentError wrong number of arguments (1 for 3..4)ArgumentError 参数数量错误(1 代表 3..4)
【发布时间】:2015-05-25 21:22:05
【问题描述】:

在我的视图/-/new.html.erb 文件中,第 3 行 出现 ArgumentError,其中指出:

“参数数量错误(1 代表 3..4)”

<div class='form-group'>
    <%= form.label :category %>
    <%= form.select "category", options_from_collection_for_select([{1 => 'Food'}, {2 => 'Entertainment'}]) %>
</div>

应用程序跟踪状态:

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'

erb:14 是上面的第 3 行erb:5

<%= form_for @event do |form| %>

【问题讨论】:

    标签: html ruby-on-rails ruby arguments erb


    【解决方案1】:

    options_from_collection_for_select([{1 =&gt; 'Food'}, {2 =&gt; 'Entertainment'}])正在抛出错误消息

    [{1 =&gt; 'Food'}, {2 =&gt; 'Entertainment'}] 是一个数组,作为一个参数传递给options_from_collection_for_select 方法;因此错误消息。

    调用options_from_collection_for_select辅助方法的正确形式是

    options_from_collection_for_select(collection, value_method, text_method, selected = nil)
    

    http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select查看更多详细信息和使用示例

    【讨论】:

      【解决方案2】:

      我认为您最好使用options_for_select,它可以与您提供的参数一起使用,只需替换代码中的options_from_collection_for_select。可以在here找到其文档。

      您需要决定是希望数字(1、2 等)还是单词(食物、娱乐等)成为到达后端的“价值”。例如:

      options_for_select({'Food' => 1, 'Entertainment' => 2})
      

      以上内容在 HTML 中的输出如下:

      <option value="1">Food</option>
      <option value="2">Entertainment</option>
      

      options_from_collection_for_select 方法需要 3 个参数。您只提供了一个:[{1 =&gt; 'Food'}, {2 =&gt; 'Entertainment'}]。您的第二个参数应该是集合中代表“值”的每个对象的方法/属性,第三个参数应该是该值的标签。

      【讨论】:

        猜你喜欢
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多