Reputation: 1439
When I log in my user from the rails login screen, it successfully tries to redirect me to the user's profile page upon verifying the login credentials, however, for some reason I receive this error upon landing on the profile page (show.html.erb):
NoMethodError in Users#show <%= @user.email %> in show.html.erb
I'm not sure why, as the email field is not nil in my database? I search the user in the rails console to verify this:
=> #<User id: 11, firstname: "Leila", lastname: "Law", email: "[email protected]", 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>
See my code below - thank you!
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
Console result when login attempted:
> 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"=>"[email protected]", "password"=>"[FILTERED]"},
> "commit"=>"Log in"}
> User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT 1 [["email", "[email protected]"]]
> Redirected to users/11
> Completed 302 Found in 118ms (ActiveRecord: 0.9ms)
Upvotes: 0
Views: 600
Reputation: 76774
Perhaps an oversight, but if you're redirecting to @user
, I assume you'd need to invoke @user
in 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
Upvotes: 0
Reputation: 176342
@user
is not assigned in the controller. You should change
user = User.find_by(email: params[:session][:email].downcase)
to
@user = User.find_by(email: params[:session][:email].downcase)
You'll have to update all the occurrences of user in the controller. Also notice the extra space here
params[:session][:email] .downcase
It should be
params[:session][:email].downcase
Moreover, if the session value was never set before, your controller will crash as there is no downcase
for nil.
Upvotes: 2
Reputation: 1260
It's because @user is nil, try adding @user = User.find(params[:id])
in your show action
Upvotes: 0