0

我有在我看來,下面的代碼:Ruby on Rails的params爲無

<%= 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)按鈕點擊,從submit buttonradio buttonparameters都到行動mentiond在form_for通過以上。但是如果我沒有選擇任何單選按鈕並在提交時選中,它會拋出(無返回的對象,您可能期望有一個數組實例)錯誤。但是,我想測試參數是否在我的動作中,並重定向到其他視圖。下面是我的行動

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 

爲什麼連拋了零對象錯誤時,即時通訊手之前檢查它的代碼? 請幫助

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"} 
+2

向我們顯示錯誤 – keymone

+0

@keymone這裏是錯誤NoMethodError在AuthorizationController#command_domain_selected 你有一個沒有對象,當你沒有想到它! 您可能預期了Array的一個實例。 評估nil時出錯[] Rails.root:c:/ Final/authorize Application Trace |框架跟蹤|完整跟蹤 app/controllers/authorization_controller.rb:43:在'command_domain_selected' – user1455116

+0

作爲標準約定,默認情況下應該選擇其中一個單選按鈕。 –

回答

0

這是一個猜測,但會不會是params[:command_selected]是零,你不檢查。

+0

非常感謝你:) – user1455116