简体   繁体   English

Ruby on Rails 2下拉菜单希望对选择预控制器进行验证

[英]Ruby on Rails 2 Drop Down Menus Want Validation of Selection Pre-Controller

I have a ruby on rails app that has a form. 我有一个表单的ruby on rails应用程序。 I was wondering if there is a way to make sure that a user has selected drop down menu items in both of the drop downs on this form before it is submitted and params are generated. 我想知道是否有一种方法可以确保用户在提交表单和生成参数之前,已经在该表单的两个下拉菜单中选择了下拉菜单项。 Ideally I would like to throw and error warning them to pick 1 item on each of the drop downs and re-render the form. 理想情况下,我想抛出错误并警告他们在每个下拉菜单中选择1个项目,然后重新呈现表单。

The form is below: 表格如下:

<%= form_tag compare_products_path, method: :get do |f| %>
  <%= select_tag :product_id1, options_from_collection_for_select(@products, 'id', 'name') %>
  <%= select_tag :product_id2, options_from_collection_for_select(@products, 'id', 'name') %>
  <%= f.submit %>
<% end %>

Please let me know how I can accomplish what I stated above. 请让我知道我如何完成上述要求。

SIDENOTE: I also implemented Select2 to make the form look nicer but could not find out of there is a quick validation trick in Select2 to accomplish what I said above, if there is a suggestion for that I can post the Select2 version, 旁白:我还实现了Select2来使表单看起来更好,但在Select2中没有一个快速验证技巧可以完成我上面所说的内容,如果有建议可以发布Select2版本,

Try this: 尝试这个:

<%= select_tag :product_id1, options_from_collection_for_select(@products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select...",:required=>true %>
<%= select_tag :product_id2, options_from_collection_for_select(@products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select..." ,:required=>true %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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