如何在生成的表单中添加选择(下拉列表)?

时间:2022-09-28 20:22:24

How can I add a select field in a form if that is generated for a model?

如果为模型生成,如何在表单中添加选择字段?

My controller sends the array that should be used to populate the select.

我的控制器发送应该用于填充选择的数组。

Controller:

def new
  @categories= Category.all
  @product= Product.new
end

The Category model has an id and a label.

Category模型具有id和标签。

The form looks like:

表格如下:

=form_for :product do |f|
- if @product.errors.any?
#error_explanation
  %h2
    = pluralize(@product.errors.count, "error")
    prohibited
    this product from being saved:
  %ul
    - @product.errors.full_messages.each do |msg|
      %li= msg
%p
  = f.label :title
  %br
  = f.text_field :title
%p
 = f.label :category_id
 %br
 = f.xxxxxxxxx
%p
  = f.submit

xxxxxxxxx is the place where I need the select populated by the @categories array.

xxxxxxxxx是我需要@categories数组填充的select的地方。

2 个解决方案

#1


2  

= f.select :category_id, @categories.collect {|c| [ c.name, c.id ]}

where @categories is Category.all

其中@categories是Category.all

#2


1  

I'd avoid @NARKOZ suggestion for two reasons. The most important is that it embeds logic that should be in the controller (fetching the Category records) into the view. That's poor separation. Second, there's a very convenient collection_select method that accomplishes the same thing.

出于两个原因,我会避免@NARKOZ的建议。最重要的是它将应该在控制器中的逻辑(获取类别记录)嵌入到视图中。那分离不好。其次,有一个非常方便的collection_select方法可以完成同样的事情。

= f.collection_select :category_id, @categories, :id, :name, {prompt: 'Pick a category'}, { class: 'select-this' }

This assumes that you have loaded a @categories instance variable in the controller as you had in your question.

这假定您已在控制器中加载@categories实例变量,就像您在问题中一样。

Note that this method takes two optional hashes at the end. The first hash accepts the usual options for the select method. The second hash accepts HTML options (e.g., HTML style, class, etc., attributes).

请注意,此方法最后需要两个可选的哈希值。第一个哈希接受select方法的常用选项。第二个散列接受HTML选项(例如,HTML样式,类等,属性)。

#1


2  

= f.select :category_id, @categories.collect {|c| [ c.name, c.id ]}

where @categories is Category.all

其中@categories是Category.all

#2


1  

I'd avoid @NARKOZ suggestion for two reasons. The most important is that it embeds logic that should be in the controller (fetching the Category records) into the view. That's poor separation. Second, there's a very convenient collection_select method that accomplishes the same thing.

出于两个原因,我会避免@NARKOZ的建议。最重要的是它将应该在控制器中的逻辑(获取类别记录)嵌入到视图中。那分离不好。其次,有一个非常方便的collection_select方法可以完成同样的事情。

= f.collection_select :category_id, @categories, :id, :name, {prompt: 'Pick a category'}, { class: 'select-this' }

This assumes that you have loaded a @categories instance variable in the controller as you had in your question.

这假定您已在控制器中加载@categories实例变量,就像您在问题中一样。

Note that this method takes two optional hashes at the end. The first hash accepts the usual options for the select method. The second hash accepts HTML options (e.g., HTML style, class, etc., attributes).

请注意,此方法最后需要两个可选的哈希值。第一个哈希接受select方法的常用选项。第二个散列接受HTML选项(例如,HTML样式,类等,属性)。