Ruby on Rails -- f.select 中的多项选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4864513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:16:05  来源:igfitidea点击:

Ruby on Rails -- multiple selection in f.select

ruby-on-rails

提问by Kim

I have the following select box in my form:

我的表单中有以下选择框:

Related Type: &nbsp; <%= f.select(:TYPE, [['Type A', 'Type A'],
                                  ['Type B', 'Type B'],
                                  ['Type C', 'Type C'],
                                  ['Type D', 'Type D'],
                                  ['Type E', 'Type E']
                                 ],{ :prompt => "Please select"}
                                 ) %>

I want to allow the user to make multiple selections and also make the size of the select box 5.

我想允许用户进行多项选择,并将选择框的大小设置为 5。

How to do that for the code above?

如何为上面的代码做到这一点?

回答by mikej

After your { :prompt => "Please select"}add another hash with html options e.g.

在您{ :prompt => "Please select"}添加另一个带有 html 选项的哈希后,例如

<%= f.select(:TYPE, [['Type A', 'Type A'],
                                  ['Type B', 'Type B'],
                                  ['Type C', 'Type C'],
                                  ['Type D', 'Type D'],
                                  ['Type E', 'Type E']
                                 ],{ :prompt => "Please select"},
                                   { :multiple => true, :size => 5 }
                                 ) %>

Once you've done this you might want to move your :promptoption (keep the empty {}though so that html attributes don't get treated as Rails options.)

完成此操作后,您可能希望移动您的:prompt选项({}尽管保留为空,以便 html 属性不会被视为 Rails 选项。)

Also you'll need to ensure your controller code is correctly accepting and handling multiple values.

此外,您还需要确保您的控制器代码正确接受和处理多个值。

回答by Swathi

In case of collection, try

在收集的情况下,尝试

    <%= f.select(:TYPE, Categories.collect {|p| [ p.name, p.id ] }, 
                                           { :prompt => "Please select"}, 
                                           { :multiple => true, :size => 5 }) %>

回答by Augustin Riedinger

I have a fully working example (including preselection when editing the object), when:

我有一个完整的示例(包括编辑对象时的预选),当:

  • Objectis the considered object
  • similar_idsis the key to relations, and is a string
  • Object是被考虑的对象
  • similar_ids是关系的关键,是一个 string

In the form:

在形式:

form_for(@object) do |f|
  = f.select :similar_ids, options_from_collection_for_select(Object.all, :id, :name, {:selected => @object.similar_ids.split(';')}), {}, {:multiple => true, :size => 4, :name => 'object[similar_ids][]'}

And in the Object.rbmodel:

Object.rb模型中:

class Object < ActiveRecord::Base
  before_save :handle_similars

  def handle_similars
    self.similar_ids = self.similar_ids.select(&:present?).join(';') 
    # .select(&:present?) is necessary to avoid empty objects to be stored
  end

  def similars
    self.class.find(self.similar_ids.split(';'))
  end

end

These posts helped me out:

这些帖子帮助了我:

Hope it helps

希望能帮助到你

回答by Sanjay Choudhary

HTML

HTML

<%= form.select(:product_ids, Product.all.collect {|p| [ p.name, p.id ] }, 
                                                   { :prompt => "Please select"}, 
                                                   { :multiple => true, :size => 5  }) %>

Controller

控制器

@category = Category.new(category_params) 

def category_params
    params.require(:category).permit(:name, product_ids: [])
end

回答by ysk

{ :prompt => "Please select"}, { :multiple => true, :size => 5 } {} is important when f.select

{ :prompt => "Please select"}, { :multiple => true, :size => 5 } {} 在 f.select 时很重要