Ruby on Rails: Params Is Nil. Undefined Method '[]' for Nil:Nilclass

Ruby on Rails: params is nil. undefined method `[]' for nil:NilClass

your problem is that params[:technols] is nil and you're trying to retrieve value of id key from a nil object. You need to instead do -

tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?

undefined method `[]' for nil:NilClass with rails params

I think you will always have params of some type, at least the action and controller.

Therefore your code is always going to be run (i.e. your if params conditional is always going to be true).

In your code you could throw in a params.inspect to see what exactly the params hash contains.

Maybe something like this would work for you:

<% if params.has_key?(:user) %>
<% if params['user']['type'] == 'Student' %>
... etc ...
<% else if params['user']['type'] == 'Employer' %>
... etc ...
<% else %>
... default ...
<% end %>
<% end %>

Although this code could probably be cleaned up using a presenter or decorator like the draper gem or something like on this Railscast:

http://railscasts.com/episodes/287-presenters-from-scratch.

Or even a helper method would be preferable to this kind of logic in the view layer.

NoMethodError undefined method ` ' for nil:NilClass

Your show action is private, therefore the instance variable @post cannot be used in the view. Move it to a public scope and the problem should be fixed.

Rails undefined method for nil:NilClass

Looking at your form, I don't think you have params[:login]. So it'll be nil and params[:login][:password] is evaluated to nil[:password]. That's why you have the error.

What you have, according to the form, should be params[:session][:login] and params[:session][:password]. So the only remaining question is how you implemented your User.find_by_login method.

undefined method `gsub' for nil:NilClass - Request Parameters: None

params[:filter] is nil and therefore you cannot call gsub on it. An blank filter seems to fall into the same category as if the filter was Select Filter therefore I would change the code to:

if params[:filter].blank? || params[:filter] == 'Select Filter'
@places = results
else
# 'Art Gallery' -> 'Art_Gallery' -> 'art_gallery' -> :art_gallery
symbol = params[:filter].gsub(/ /, '_').downcase.to_sym
# @places = results.where(:art_gallery => true)
@places = @places.where(symbol => true)
end

undefined method `[]' for nil:NilClass + after login

The parameters you are sending are incorrect, you are taking the params value as a string, which is incorrect,

Try sending params as key and value,

Send Parameters as:

{

"utf8"=>"✓",
"authenticity_token"=>"b7t1yJ7cGyxDmecBpvBrGLRbh/ubtPjsUFdP/HosK8kp/ZLRtrV3dVWE2vPjDnThCbt9hk1ftm5ZMg9Ia1StWw==",
"email"=>{"pavanez4u@gmail.com",
"tabIndex"=>"1",
"password"=>"[FILTERED]"
"commit"=>"Log In"
}

@email = params[:email] this line throws you an error because the params format is incorrect.

Rails undefined method * for nil:NilClass

I have solved the problem, Thank you for the suggestions, I was using the wrong variable, I was using @question, When because its nested, the correct variable is @members_question

Submissions Controller

def create

@members_question = Members::Question.find(params[:question_id])
@submission = @members_question.submissions.create(params[:submission].permit(:content, :question_id))
@submission.member_id = current_member.id

end

_form.html.erb

<%= form_for([@members_question, @members_question.submissions.build]) do |f| %>

<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

Why do I get undefined method for nil:NilClass on an object that clearly exists?

I'm pretty sure that the record you inspect (Daily with id=1030) and the failing [non-existing] record are two different records. Here's how you can find out. Amend your method like this, temporarily:

def daily_data_for(store, month, day, year, field)
daily = Daily.where(store_id: store, month: month, day: day, year: year).first
raise "Daily not found for #{store.inspect}, #{month.inspect}, #{day.inspect}, #{year.inspect}" unless daily
daily.send(field)
end

Now the error message should be much more revealing.

Undefined method 'each' for nil:NilClass, for @topics

This error just means that @topics is empty.

To troubleshoot this, you need to refresh the page while watching your console.

Examine the queries being ran when these lines run:

@topics = Topic.search(params[:search]).paginate(:page => params[:page], :per_page => 5
@topics = Topic.all

If you can't spot the issue in the console, copy the query into your database GUI and run the query, modify it till you get your expected results and edit the query.

I would assume that your issue might have to do with the params being passed.

You should add this before your queries to view what params you are picking up.

p 'my params'
p params[:search]
p params[:page]

PS. I suggest making your per_page a variable, it makes it easier when going through code to see all the hard coded values and change them when they are not in the middle of a code block.



Related Topics



Leave a reply



Submit