![](/img/trans.png)
[英]How can i add form_select to some fixed drop-down in rails app
[英]How to get some fixed static values in rails form tag drop down?
我正在嘗試實現過濾器。 我有一個帶有列status
的模型,並且為該列定義了枚舉狀態,以便使其可讀。 並且這些狀態是固定的。
模型
class Production < ApplicationRecord
enum status:{
Preproduction:1,
Postproduction: 2,
Completed:3
}
end
而且我正在嘗試根據用戶給出的狀態來過濾表。 僅當用戶輸入狀態的完整名稱(如preproduction
才過濾結果。 控制器如下:
調節器
def index
if params[:filter]
@productions = Production.where('productions.status like ?', "%#{Production.statuses[params[:filter].capitalize]}%")
else
@productions = Production.all
end
end
如您所見,我正在使用參數進行查詢。
我當前的表單標簽如下所示
指數
<%= form_tag productions_path, :method => 'get', :id=> "contacts_search" do %>
<p>
<%= text_field_tag :filter, params[:filter] %>
<%= submit_tag "Filter", :name => nil %>
</p>
<div id="cresults_div"><%= render 'cresults'%> </div>
<% end %>
我有一些固定的狀態,如下所示:
Production.statuses => {"Preproduction"=>1, "Postproduction"=>2, "Completed"=>3}
由於我是在params的幫助下查詢控制器的,因此無法像某些答案中所建議的那樣直接使用f.select
。 如何在此文本字段的廣告下拉菜單中獲取這些關鍵字“ Preproduction
Postproduction
Completed
?
<%= select_tag :'filter', options_for_select(Production.statuses, params[:filter]), include_blank: '--Select One Option--' %>
在控制器中:-
def index
if params[:filter]
@productions = Production.where(status: params[:filter])
else
@productions = Production.all
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.