when log in user rails login screen, tries redirect me user's profile page upon verifying login credentials, however, reason receive error upon landing on profile page (show.html.erb):
nomethoderror in users#show <%= @user.email %> in show.html.erb
i'm not sure why, email field not nil in database? search user in rails console verify this:
=> #<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>
see 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 successfully" flash[:color] = "valid" redirect_to @user else flash[:notice] = "form 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 |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" 96.49.105.234 @ 2016-01-24 19:50:40 +0000 > processing sessionscontroller#create 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".* "users" "users"."email" = ? limit 1 [["email", "leila@gmail.com"]] > redirected users/11 > completed 302 found in 118ms (activerecord: 0.9ms)
@user
not assigned in controller. should change
user = user.find_by(email: params[:session][:email].downcase)
to
@user = user.find_by(email: params[:session][:email].downcase)
you'll have update occurrences of user in controller. notice space here
params[:session][:email] .downcase
it should be
params[:session][:email].downcase
moreover, if session value never set before, controller crash there no downcase
nil.
Comments
Post a Comment