将select_tag引入使用范围进行简单过滤

时间:2018-10-22 11:46:41

标签: ruby-on-rails ruby

我仍在学习Rails,在我的项目应用程序中,管理员应该能够使用下拉列表(已批准为true,未批准为false)按状态(db中的布尔值字段)过滤所有假期请求。我想我已经在我的控制器和模型中声明了所有正确的内容,但是我不知道如何在视图中实现这一点。我正在尝试跟进Austin's story blogUsing select_tag multiple => true to get a combined scope主题,但我仍然不知道该怎么做。

离开控制器:

def index
 @leave = Leave.new
 @leaves = Leave.all.order(created_at: :desc).includes(:user)

 @leaves = Leave.find_by('select = ?', params[:status])
end

模型leave.rb

scope :approved, -> { where(status: true) }
scope :pending, -> { where(status: false) }

belongs_to :user, optional: true

查看index.html.erb

<%= form_tag(action: :index) do %>
 <div class="input-group">
  <%= select_tag :status, options_for_select([['Approved', Leave.approved], ['Pending', Leave.pending]]) %>
  <%= submit_tag 'Filter' %>
 </div>
<% end %>

1 个答案:

答案 0 :(得分:1)

将代码更改为此

{{1}}