Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError in Pages#home

I'm working through the railtutorial.org online book for rails 3. I've made it through most of chapter 11, where we add the ability to submit a micropost. After adding the appropriate code, I'm unable to render the page. The following is the error returned:
>

NoMethodError in Pages#home

Showing c:/rails_projects/sample_app/app/views/shared/_error_messages.html.erb where line >#1 raised:

You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.errors Extracted source (around line #1):

1:<% if @user.errors.any? %>
2:<div id="error_explanation">
3:<h2><%= pluralize(@user.errors.count, "error") %>
4:prohibited this <%= object.class.to_s.underscore.humanize.downcase %>
Trace of template inclusion: app/views/shared/_micropost_form.html.erb, >app/views/pages/home.html.erb

The page will render correctly if I remove the following line from app\views\shared_micropost_form.html.erb <%= render 'shared/error_messages', :object => f.object %>

Any help is appreciated.

like image 413
alphanumericone Avatar asked Jan 22 '23 06:01

alphanumericone


1 Answers

it's because you're passing a variable object into your partial, but in the partial you're trying to use a variable called @user. Change each instance of @user in that partial to object and it will work fine.

1:<% if object.errors.any? %>
2:<div id="error_explanation">
3:<h2><%= pluralize(object.errors.count, "error") %>
4:prohibited this <%= object.class.to_s.underscore.humanize.downcase %>

UPDATE: Just to clarify, the answers above are assuming there's a fault with setting the @user variable, but it's fine. When you say :object => f.object in your render call, you're telling render to take the object that this form is based on, and send it to the partial - with the variable name object.

The whole point of refactoring the error code into a shared partial is that it will be used by multiple forms, for different models. Inside the partial you can't say @user because you will be using this same partial for all your other models. That's why the code in the partial is changed to use a more generic variable name, object.

like image 91
Jaime Bellmyer Avatar answered Jan 31 '23 06:01

Jaime Bellmyer