【问题标题】:Ruby on Rails -- multiple selection in f.selectRuby on Rails -- f.select 中的多项选择
【发布时间】:2011-06-19 08:50:24
【问题描述】:

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

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"}
                                 ) %>

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

上面的代码怎么做?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    在您的 { :prompt =&gt; "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 }
                                     ) %>
    

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

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

    【讨论】:

    • 为什么要为存储在 :TYPE 中的每个未选择的选项添加“---”和“-”?
    • 当我以表格形式保存它时,在位置 0 "ids" => [ [0] "", [1] "some_id" ]
    【解决方案2】:

    万一收藏,试试

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

    【讨论】:

    • 不应该是Category.all.collect吗?
    【解决方案3】:

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

    • Object 是考虑的对象
    • similar_ids 是关系的关键,是string

    形式:

    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][]'}
    

    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
    

    这些帖子帮助了我:

    希望对你有帮助

    【讨论】:

    • 名称以[] 结尾很重要,以便rails 将参数解释为值数组
    【解决方案4】:

    HTML

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

    控制器

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        使用引导选择选择器和预选值:

            = simple_form_for [:backend, @user], html: { autocomplete: 'off' } do |f|
              = f.select :role_ids, options_for_select(Role.all.map{|role| [role.name, role.id]}, @user.role_ids), {},  {:multiple => true, inlcude_blank: false, class: "form-control input-sm selectpicker"}
        

        在控制器中:

        def user_params
              params.require(:user).permit(:id, role_ids: [])
        end
        
        # only if you havent build in new action
        def new
          # set user
          @user.roles.any?
        end
        

        【讨论】:

          【解决方案7】:
          <%= f.select :tag_ids, Tag.all.collect {|t| [t.name, t.id]}, { :prompt => "Please select"}, { :multiple => true, :size => 5 } %>
          

          【讨论】:

          • 请考虑添加一些解释,以便 OP 和未来的读者知道您的代码为什么以及如何解决手头的问题。
          猜你喜欢
          • 1970-01-01
          • 2011-06-30
          • 2023-03-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多