Rails 4 - 按范围收集简单表单
Rails 4 - Simple Form collection by scope
我正在尝试在 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>
谁能看出哪里出了问题?
应该是alphabetically
而不是alphabetical
。
此外,根据 options_from_collection_for_select 文档,您需要将至少 3 个参数传递给 options_from_collection_for_select
方法:collection
、value_method
和 text_method
.
更改为以下内容以使其工作:
<%= f.select :sector, options_from_collection_for_select(Industry.alphabetically, 'id', 'sector'), :prompt => 'Select' %>
我正在尝试在 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>
谁能看出哪里出了问题?
应该是alphabetically
而不是alphabetical
。
此外,根据 options_from_collection_for_select 文档,您需要将至少 3 个参数传递给 options_from_collection_for_select
方法:collection
、value_method
和 text_method
.
更改为以下内容以使其工作:
<%= f.select :sector, options_from_collection_for_select(Industry.alphabetically, 'id', 'sector'), :prompt => 'Select' %>