验证至少并且仅选择一个attr_accessible

时间:2013-12-11 16:56:50

标签: ruby-on-rails ruby ruby-on-rails-3 validation

Ruby on Rails 3。 有两个问题的表格。我只希望用户回答其中一个。你如何验证这个? 问题:

<div class="row" id="phonepbx" style=""> 
                                <ul>4a.  My customers have their own brand preferences regarding VoIP manufacturers.<br/>
<li><%= f.radio_button(:voip, "Strongly Agree") %>Strongly Agree</li>
<li><%= f.radio_button(:voip, "Somewhat Agree") %>Somewhat Agree</li>
<li><%= f.radio_button(:voip, "Neutral") %>Neutral</li>
<li><%= f.radio_button(:voip, "Somewhat Disagree") %>Somewhat Disagree</li>
<li><%= f.radio_button(:voip, "Strongly Disagree") %>Strongly Disagree</li>
</ul>
</div>                              

<div class="row">
<ul>4b.  My customers have their own brand preferences regarding IP Surveillance manufacturers.<br/>
<li><%= f.radio_button(:surv, "Strongly Agree") %>Strongly Agree</li>
<li><%= f.radio_button(:surv, "Somewhat Agree") %>Somewhat Agree</li>
<li><%= f.radio_button(:surv, "Neutral") %>Neutral</li>
<li><%= f.radio_button(:surv, "Somewhat Disagree") %>Somewhat Disagree</li>
<li><%= f.radio_button(:surv, "Strongly Disagree") %>Strongly Disagree</li>
</ul>
</div>

我目前在Survey.rb模型中验证:

 class Survey < ActiveRecord::Base
 attr_accessible :comments, :surv, :hardware, :voip, :quality, :revenue, :ucmrating, :noucm, :survey_taken, :user_id
 belongs_to :user

 validates :hardware, presence: true

 validates :revenue, presence: true
 validates :ucmrating, presence: true
 validates :noucm, presence: true
 validates :quality, presence: true

 validate :at_least_one_name

 def at_least_one_name
    if voip.blank? and surv.blank?

       return false
      elsif !voip.blank? and !surv.blank?

       return false
      else
       return true
     end   
  end
end

控制器

class SurveysController < ApplicationController
  def create
params[:survey][:user_id] = current_user.id
params[:survey][:survey_taken] = true
@survey = Survey.new(params[:survey])
    if @survey.save
      flash[:success] = "Thank you for taking our survey."
      redirect_to current_user
    else
      flash[:error] = "Sorry, please fill out all of the survey questions."
      redirect_to current_user
    end
  end
end

1 个答案:

答案 0 :(得分:1)

如果我理解正确:
你需要检查选择的任何答案。这是在客户端完成的。你需要一个隐藏按钮的简单javascript。或者在控制器级别检查。
这只是一个例子。controller级别检查:

class SurveysController < ApplicationController
  def create
    params[:survey][:user_id] = current_user.id
    params[:survey][:survey_taken] = true
    @survey = Survey.new(params[:survey])
    if validation  # <- return true if any params have
      if @survey.save
        flash[:success] = "Thank you for taking our survey."
        redirect_to current_user
      else
        flash[:error] = "Sorry, please fill out all of the survey questions."
        redirect_to current_user
      end
    end
  end

  private
  # Check if no one not selected
  def validation
    params[you_form][:voip].empty? && params[you_form][:surv].empty? ? false : true
  end
end