0

在我看来,我有以下代码:

<%= form_for :command_selected, :method => "get", :url => {:action => "command_domain_selected"} do |f| %>

   <%= f.submit "Make Deployment" %>
<%= f.radio_button :domain, '1dev-den' %> <%= label :domain, '1dev-den'%><br />
   <%= f.radio_button :domain, '2dev-den' %> <%= label :domain, '2dev-den'%><br />

当我单击submit (make deployment)按钮时,parameterssubmit buttonradio button传递到form_for上面提到的操作。但是,如果我没有选择任何单选按钮并在提交时 cilck,它会抛出(返回的 nil 对象,您可能期望一个数组实例)错误。但是,我想测试参数在我的操作中是否为 nil 并重定向到其他视图。以下是我的操作代码

def command_domain_selected
  if(params[:command_selected][:domain].nil?)
    respond_to do |format|
      format.html{redirect_to wrong_selection_path}
    end
  else
    @command_selected = params[:commit]      
    @domain_selected = params[:command_selected][:domain]
  end 
end

为什么即使我事先检查它也会抛出 nil 对象错误?请帮忙

Update: Below is the error i'm getting
NoMethodError in AuthorizationController#command_domain_selected 
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]Rails.root: c:/Final/authorize

Parameters: 

{"utf8"=>"✓", "commit"=>"Make Deployment"}
4

1 回答 1

0

这是一个猜测,但它可能params[:command_selected]是你没有检查的 nil 。

于 2012-07-03T19:06:13.860 回答