简体   繁体   中英

NoMethod error in ruby on rails

I am following a tutorial where we are creating and updating user accounts, and Im stuck on the edit part as when I try to load the edit page for a user I get a no method error as shown below:

Showing /home/ubuntu/rails_projects/alpha_blog/app/views/shared/_errors.html.erb where line #1 raised:

undefined method `errors' for nil:NilClass
Extracted source (around line #1):
1
2
3
4
5
6

<% if obj.errors.any? %>
  <div class="row">
    <div class="col-xs-8 col-xs-offset-2">
      <div class="panel panel-danger">
        <div class="panel-heading">
          <h2 class="panel-title">

Trace of template inclusion: app/views/users/_form.html.erb, app/views/users/edit.html.erb

Rails.root: /home/ubuntu/rails_projects/alpha_blog

Application Trace | Framework Trace | Full Trace
app/views/shared/_errors.html.erb:1:in `_app_views_shared__errors_html_erb__3874437342468649522_70028100096200'
app/views/users/_form.html.erb:1:in `_app_views_users__form_html_erb__2653828291511096048_70028100213300'
app/views/users/edit.html.erb:4:in `_app_views_users_edit_html_erb___925607472456440074_70028151623400'
Request
Parameters:

{"id"=>"2"}
Toggle session dump
Toggle env dump
Response
Headers:

None

Here is my users controller:

class UsersController < ApplicationController
  def new
    @user= User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      flash[:success] = "Welcome to the alpha blog #{@user.username}"
      redirect_to articles_path
    else
      render 'new'
    end

  end

  private

  def user_params

    params.require(:user).permit(:username, :email, :password)
  end

  def edit
  @user = User.find(params[:id])

  end 

  def update
  @user = User.find(params[:id])
    if @user.update(user_params)
      flash[:success] = "Your account was updated successfully"
      redirect_to articles_path
    else

      render 'edit'
    end


  end




end

Since I initialized the variable @user in the edit class, I thought this would work.

In file _app/views/users/ form.html.erb , while rendering _shared/ errors.html.erb partial, also pass obj as local to it. This is how you can do it:

<%= render 'shared/errors', obj: @user %>

If it doesn't work, please post users/edit and users/_form templates too.

Here are 2 answers to your question

  1. If you are using " Devise " gem, try accessing it using " resource.errors " instead of obj.errors

  2. If you are making your custom form, so basically in your partial , Object is not found. So from wherever this partial is rendering, you can try using the below code. So you are trying to access error for obj, which is a nil class. Try passing the locals as below

    render(:partial => 'users/feeds', :locals => { :obj => @user })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM