2013-02-27 32 views
2

我有下拉列表和两个单选按钮的form.here是code.i不知道如何设置验证在controller.when不从下拉选择值和一个的单选按钮,我必须显示警告消息'请从下拉列表中选择值'并检查一个单选按钮filed.how可以在提交表单时在rails上运行ruby。在轨道上的红宝石下拉列表表单验证警报

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

在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 
+0

你有2个选项,你可以使用JavaScript显示警告信息,也可以显示提示信息错误。 – 2013-02-27 10:43:08

回答