用户#中的NoMethodError显示错误?

时间:2016-01-24 19:59:04

标签: html ruby-on-rails ruby-on-rails-4

当我从rails登录屏幕登录我的用户时,它在验证登录凭据时成功尝试将我重定向到用户的个人资料页面,但是,出于某种原因,我在登录个人资料时收到此错误页面( show.html.erb)

  用户中的

NoMethodError#show <%= @ user.email%>在show.html.erb

我不知道为什么,因为我的数据库中的电子邮件字段不是nil?我在rails控制台中搜索用户以验证这一点:

 => #<User id: 11, firstname: "Leila", lastname: "Law", email: "leila@gmail.com", created_at: "2016-01-24 19:49:13", updated_at: "2016-01-24 19:49:13", password_digest: "$2a$10$WDTN0YlUr3W/zNfAhymZzuKvZiQL1praJowYtkK3v.6...", password: nil>

请参阅下面的代码 - 谢谢!

show.html.erb

<setheading>Personal Information</setheading>

<%= @user.email %>

sessions_controller.rb

   class SessionsController < ApplicationController


  def new
  end

  def create


   @user = User.find_by(email: params[:session][:email].downcase)

   if @user && @user.authenticate(params[:session][:password])

     session[:user_id] = @user.id



    # log_in user
     redirect_to @user

   else

   #  flash[:danger] = 'Invalid email/password combination' #Not quite right!
     render 'new'

     end
  end

def destroy
end
end

users_controller.rb

class UsersController < ApplicationController

def create
  @user = User.new(user_params)
  if @user.save
    flash[:success] = "You signed up successfully"
    flash[:color] = "valid"
    redirect_to @user
  else
    flash[:notice] = "Form is invalid"
    flash[:color] = "invalid"
    render "new"
  end
end



private
def user_params
  params.require(:user).permit(:firstname, :lastname, :email, :password, :password_confirmation)
end


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

def new
end

end

Schema.db

  create_table "users", force: :cascade do |t|
    t.string   "firstname"
    t.string   "lastname"
    t.string   "email"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "password_digest"
    t.string   "password"
  end

尝试登录时的控制台结果:

>  Started POST "/login" for 96.49.105.234 at 2016-01-24 19:50:40 +0000
>     Processing by SessionsController#create as HTML
>       Parameters: {"utf8"=>"✓", "authenticity_token"=>"JYAY6VQi6FOuPsZXmLhfVjIcBtC0DLrwKY+9SKBAhgmfb+0rOKWsWGK9s+YSTHZTtuTK6LUVpxExX2Y2mHlW0A==",
> "session"=>{"email"=>"leila@gmail.com", "password"=>"[FILTERED]"},
> "commit"=>"Log in"}
>       User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = ? LIMIT 1  [["email", "leila@gmail.com"]]
>     Redirected to users/11
>     Completed 302 Found in 118ms (ActiveRecord: 0.9ms)

3 个答案:

答案 0 :(得分:2)

控制器中未分配

@user。你应该改变

 user = User.find_by(email: params[:session][:email].downcase)

 @user = User.find_by(email: params[:session][:email].downcase)

您必须更新控制器中所有用户的更新。另请注意这里的额外空间

params[:session][:email] .downcase

应该是

params[:session][:email].downcase

此外,如果以前从未设置会话值,则控制器将崩溃,因为没有downcase为nil。

答案 1 :(得分:0)

这是因为@user为零,尝试在您的节目动作中添加@user = User.find(params[:id])

答案 2 :(得分:0)

也许是一种疏忽,但如果您重定向@user,我认为您需要在@user中调用users#show

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def show
      @user = User.find params[:id] #-> id will have been sent by redirect_to @user
   end
end