根据参数选择选择字段
<%= 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下操作:
选择
方法在选项中选择选择作为选择的选项。当为模型生成
form
时,该方法将自动选择选定选项。如果需要,您也可以使用select_tag
像这样:Try this:
The
select
method acceptsselected
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 useselect_tag
like this if you wish:使用参数
选择
您可以设置应选择的值。With the parameter
selected
you can set the value which should be selected.