繁体   English   中英

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

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

我有一个表单的ruby on rails应用程序。 我想知道是否有一种方法可以确保用户在提交表单和生成参数之前,已经在该表单的两个下拉菜单中选择了下拉菜单项。 理想情况下,我想抛出错误并警告他们在每个下拉菜单中选择1个项目,然后重新呈现表单。

表格如下:

<%= 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 %>

请让我知道我如何完成上述要求。

旁白:我还实现了Select2来使表单看起来更好,但在Select2中没有一个快速验证技巧可以完成我上面所说的内容,如果有建议可以发布Select2版本,

尝试这个:

<%= 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