【问题标题】:Rails 4 - Simple Form collection by scopeRails 4 - 按范围的简单表单集合
【发布时间】:2016-01-03 06:39:04
【问题描述】:

我正在尝试在 Rails 4 中制作应用程序。

我使用简单的表单。

我有一个行业模型。

industry.rb 有:

scope :alphabetically, -> {  order("sector ASC") }

行业控制者有:

def index
    #@industries = Industry.all
    @industries = Industry.alphabetically
  end

行业形态有:

<%= simple_form_for(@industry) do |f| %>
            <%= f.error_notification %>

                <div class="form-inputs">


                    <%= f.select :sector, options_from_collection_for_select(Industry.alphabetical), :prompt => 'Select' %>  

                    <%= f.input :icon, as: :file, :label => "Add an icon" %>

                </div>

                <div class="form-actions">
                    <%= f.button :submit, "Submit", :class => 'formsubmit' %>
                </div>
        <% end %>

我正在尝试为 :sector 获取表单输入以使用行业集合(通过调用范围)。

当我尝试这个时,我收到以下错误:

undefined method `alphabetical' for #<Class:0x007fef65635220>

谁能看出问题所在?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 collections scope simple-form


    【解决方案1】:

    应该是alphabetically 而不是alphabetical。 另外,根据options_from_collection_for_select 文档,您需要将至少3 个参数传递给options_from_collection_for_select 方法:collectionvalue_methodtext_method

    更改以下内容以使其工作:

    <%= f.select :sector, options_from_collection_for_select(Industry.alphabetically, 'id', 'sector'), :prompt => 'Select' %>
    

    【讨论】:

    • 谢谢 - 它给出了 - 参数数量错误(1 代表 3..4)
    • 是的,您至少需要将 3 个参数传递给 options_from_collection_for_select :collection、value_method、text_method。看到这个:apidock.com/rails/v4.2.1/ActionView/Helpers/FormOptionsHelper/… 和我更新的答案。但是,第一个错误是由于拼写错误。
    • 名称字段应该包含什么?当我按原样尝试时,我收到此错误:#<0x007fef4b2b37d8>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    相关资源
    最近更新 更多