【问题标题】:Ransack on enum filter for dropdown list搜索下拉列表的枚举过滤器
【发布时间】:2022-02-24 20:36:20
【问题描述】:
class Schedule < ApplicationRecord 
  belongs_to :event
  enum county: {USA: 0, INDIA: 1, Brasil: 2}
end

在 index.erb.html 中搜索选择

<%= f.collection_select :schedules_county_matches_all, Schedule.counties.map{ |dp| [dp.first, dp.first.humanize] }, :first.to_s, :second ,:include_blank => "All"%>

它给出了输出选择下拉列表,但不是 Postgres 数据库中的值存储为整数而不是字符串的值

<li class=""><span>All</span></li>
<li class=""><span>USA</span></li>
.......

作为工作过滤器,我想它应该在 li 中增加价值

<li value=""><span>All</span></li>
<li value="0"><span>USA</span></li>
.......

【问题讨论】:

  • 为了将其保存为county 值需要多少值?您应该传递国家值,它是一个整数,并且只使用键来显示它。跨度>

标签: ruby ruby-on-rails-3 ruby-on-rails-4 ransack


【解决方案1】:

使用下面的代码来解决您的问题

<%= f.select :schedules_county_matches_all, Schedule.counties.map { |r| [r[0], r[1].to_i] }, include_blank: true %>

【讨论】:

    【解决方案2】:

    创建你的洗劫者。

      # user.rb
      enum role: [:Admin, :Analyst]
      ransacker :role, formatter: proc {|v| roles[v]} do |parent|
        parent.table[:role]
      end
    

    然后在你的 index.html.erb 中添加字段

    <%= f.select :role_eq, User.roles.keys , {:include_blank => 'All Roles'}, { :class => 'form-control' } %>
    

    【讨论】:

      【解决方案3】:
      <%= f.select :schedules_county_eq, options_for_select(Schedule.counties), include_blank: 'All' %>
      

      Rails 7,Ransack 2.5.0。小写字符串。

      【讨论】:

        猜你喜欢
        • 2011-10-15
        • 2018-06-12
        • 1970-01-01
        • 1970-01-01
        • 2015-10-14
        • 1970-01-01
        • 2020-06-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多