Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2

ruby on rails params is nil


I'm having the following code in my view:

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

When i click on the submit (make deployment) button, parameters from submit button and radio button are passed on to the action mentiond in the form_for above. But if i do not select any radio button and cilck on submit, it throws the (nil object returned you might be expecting an instance of array) error. However, i want to test if the parameter is nil in my action and redirect to some other view. Below is my code for the action

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

Why is it throwing the nil object error even when im checking it before hand? Please help

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

Solution

  • This is a guess but could it be that params[:command_selected] is nil which you are not checking for.