繁体   English   中英

如何在Rails Form标签下拉列表中获取一些固定的静态值?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM