简体   繁体   English

下拉列表表单验证警报在Rails中的ruby中

[英]Drop down list form validation alert in ruby on rails

I have drop down list and two radio button in a form.here is the code.i dont know how to set validations in controller.when not selecting value from drop down and one of the radio button,i have to show the warning messages 'please select value from dropdown' and check one radio button filed.how it is possible in ruby on rails while submitting a form. 我在表单中有一个下拉列表和两个单选按钮。这是代码。我不知道如何在controller中设置验证。当不从下拉列表和一个单选按钮中选择值时,我必须显示警告消息“请从下拉菜单中选择值”,然后选中一个单选按钮。提交表单时,如何在红宝石上进行操作?

<%= form_tag :action => 'show' do %>
    <strong>Select device: </strong> <%= collection_select(:device, :id, @devices, :id, :name, options ={:prompt => "-Select a device"}) %>
    <br></br>
    <strong>Chose: </strong><%=  radio_button_tag :name,:time, false, :onclick => "this.parentNode.submit();"%>Time
    <%=  radio_button_tag :name,:graph%>Graph
    <% end %> 

In device.rb i set the following but not showing any messages. 在device.rb中,我设置了以下内容,但未显示任何消息。

 class Device < ActiveRecord::Base
   attr_accessible :name
   validates_presence_of :name
   has_many :properties

   def validate
     if name == 'None'
       errors.add_to_base("You must select a device name")
     end
   end
 end

Why not use a validates_exclusion_of ? 为什么不使用validates_exclusion_of? (or its equivalent in Rails 3) (或其在Rails 3中的等效版本)

http://apidock.com/rails/v2.3.8/ActiveModel/Validations/ClassMethods/validates_exclusion_of http://apidock.com/rails/v2.3.8/ActiveModel/Validations/ClassMethods/validates_exclusion_of

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

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