根据参数选择选择字段

发布于 2025-01-24 19:45:35 字数 561 浏览 3 评论 0原文

<%= select("category", "category_id", Category.all.collect { |p| [ p.title, p.id] },{include_blank: "All Category"},{class: "form-control"}  ) %>   

<div class="col-auto"> 
  <%= button_tag( class: "btn btn-sm btn-outline-primary btn-rounded search-button-style") do %>
    <i class="fa fa-search"></i>
    <span><%= I18n.t 'search' %></span>
  <% end %>
</div>

我有一个选择字段。我需要根据参数选择数据。我将在参数中获取category_id params [:category] ​​[:category_id]

<%= select("category", "category_id", Category.all.collect { |p| [ p.title, p.id] },{include_blank: "All Category"},{class: "form-control"}  ) %>   

<div class="col-auto"> 
  <%= button_tag( class: "btn btn-sm btn-outline-primary btn-rounded search-button-style") do %>
    <i class="fa fa-search"></i>
    <span><%= I18n.t 'search' %></span>
  <% end %>
</div>

I have a select field. I need to select the data based on params. I will be getting category_id in the params as params[:category][:category_id]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在巴黎塔顶看东京樱花 2025-01-31 19:45:35

尝试以下操作:

<%= select("category", "category_id", Category.all.collect { |p| [ p.title, p.id] }, { include_blank: "All Category", selected: params[:category][:category_id] }, { class: "form-control" }) %> 

选择方法在选项中选择选择作为选择的选项。

当为模型生成form时,该方法将自动选择选定选项。如果需要,您也可以使用select_tag像这样:

<%= select_tag("category_id", options_for_select(Category.all.collect { |p| [ p.title, p.id] }, params[:category][:category_id]), include_blank: "All Category", class: "form-control") %> 

Try this:

<%= select("category", "category_id", Category.all.collect { |p| [ p.title, p.id] }, { include_blank: "All Category", selected: params[:category][:category_id] }, { class: "form-control" }) %> 

The select method accepts selected in the options to choose one as selected.

This method is useful when generating a form for a model where it would autoselect the chosen option. You can also use select_tag like this if you wish:

<%= select_tag("category_id", options_for_select(Category.all.collect { |p| [ p.title, p.id] }, params[:category][:category_id]), include_blank: "All Category", class: "form-control") %> 
情定在深秋 2025-01-31 19:45:35

使用参数选择您可以设置应选择的值。

<%= select("category", "category_id", Category.all.collect { |p| [ p.title, p.id] },{include_blank: "All Category", selected: value},{class: "form-control"} ) %> 

With the parameter selected you can set the value which should be selected.

<%= select("category", "category_id", Category.all.collect { |p| [ p.title, p.id] },{include_blank: "All Category", selected: value},{class: "form-control"} ) %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文