NoMethodError on Pages # home

I work through the railtutorial.org online book for rails 3. I did this through most of chapter 11, where we added the ability to send microposts. After adding the appropriate code, I cannot display the page. The following is the error:
>

NoMethodError on Pages # home

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

You have zero if you did not expect this! Perhaps you were expecting an instance of ActiveRecord :: Base. Error evaluating nil.errors Extracted source (around line # 1):

1 <% if @user.errors.any? %> <% 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 %>
Template inclusion trace: app / views / shared / _micropost_form.html.erb,> app / views / pages / home.html.erb

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

Any help is appreciated.

+4
source share
3 answers

because you are passing the variable object to your partial, but in partial you are trying to use a variable called @user . Change each instance of @user in 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: To clarify, the answers above suggest there is an error setting the @user variable, but this is normal. When you say :object => f.object in a render call, you tell render to take the object that this form is based on and send it partial β€” with the variable name object .

The whole point of refactoring an error code into a common partial code is that it will be used by several forms for different models. In the partial part, you cannot say @user , because you will use the same part for all your other models. Therefore, if the code is partially modified to use the more common variable name, object .

+9
source

This tells you that the @user object is zero. You must make sure that in your pages#home controller you have actually set the value to @user . If you are redirected to the home page after saving the error in the form message, then after the redirection they still will not have the @user variable. This is the most common practice if you want to include the original published values ​​in the form message error message to visualize the view, so that you still have the published parameters:

 # Some controller def create begin @user = User.create(params[:user]) rescue ActiveRecord::RecordInvalid => e @errors => e.record.full_error_messages render :action => "new" end end 

I'm not sure if you have a lesson, but if you put the code that is in page_controller # home, we could tell you more.

0
source

It seems that there is no @user variable in the controller action that you are accessing

I would try

 <% if @user && @user.errors.any? %> 

if there is @user then it will check @ user.errors.any

also, if you use the program, you can try

 <% user_signed_in? && @user.errors.any? %> 
0
source

All Articles