launchscout / nku

NKU Class Spring
5 stars 14 forks source link

Error in users controller after adding has_secure_password code #52

Closed beisert1 closed 10 years ago

beisert1 commented 10 years ago

Here is the error I get after adding the following this guide to implement has_secure_password

http://www.millwoodonline.co.uk/blog/using-hassecurepassword-for-rails-authentication

After adding all the code I am unable to update my db migrate with users table so I create a new one for sessions but I added the code from the guide in both my users model, users controller and the schema users table. As well as created a sessions model. sessions controller, and updated my sessions table successfully however, I get this following error, and I dont know how to link the user and session tables together?

image

Sparkesg1 commented 10 years ago

You have nothing in def new. I don't see a point to the session model other than I'm sure you used it to create a table, hence you don't need to have that with has_secure_password. I don't see why that error is popping up. So hopefully the prior stated might somehow magically fix it.

rake db:drop rake db:create rake db:migrate may fix your tables issue as stated in a much earlier post by a very nice gaslight guy. (this will drop all your information in the tables and may require you to clear your cookies)

And you compare the tables to each other. Something like this in your view. <% if current_user && current_user.id == user.id %>

with something like this in your application_controller.rb

def current_user @current_user ||= User.find(session[:user_id]) if session[:user_id] end helper_method :current_user

Hope this helps.

jaimerump commented 10 years ago

It found an unexpected keyword end in user.rb, it was expecting end_of_input instead. That just means that you have an extra end in there after the interpreter thought the file was done. When you were copy-pasting from the guide into your user class you picked up an extra end and it's causing a SyntaxError.

beisert1 commented 10 years ago

Thanks guys! but I don't think its a missing end or an extra one I thoroughly checked for that because that was my first guess. I tried dropping my table earlier for users but I kept getting an error when trying to migrate

rockwood commented 10 years ago

Can you paste the code from your UsersController?

beisert1 commented 10 years ago
class UsersController < ApplicationController

def new
end

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

def create
  @user = User.new(user_params)
  @user.save
  flash[:notice] = "student was successfully created."
  redirect_to @user, notice: "student was successfully created."
    user = User.find_by_email(params[:email])
  if user && user.authenticate(params[:password])
    session[:user_id] = user.id
    redirect_to admin_root_path, :notice => "Welcome back, #{user.email}"
  else
    flash.now.alert = "Invalid email or password"
    render "new"
  end
end

def index
    @users = User.all
end  

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

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

  if @user.update(params[:user].permit(:name, :nickname, :email, :image))
    flash[:notice] = "You have successfully updated your profile."
    redirect_to @user, notice: "You have successfully updated your profile."
  else
    render 'edit'
  end
end  

def destroy
  @user = User.find(params[:id])
  @user.destroy
  redirect_to users_path
end  

private
def user_params
  params.require(:user).permit(:name, :nickname, :email, :image)
end

end
camdixon commented 10 years ago

Not sure if this helps but there is a small difference I see. All of your other definitions are using @user while your def index is using the plural version @users. Something to consider / try

rockwood commented 10 years ago

Looks like your trying to do too much in your create action.

Have a look at http://railscasts.com/episodes/270-authentication-in-rails-3-1

As for the error, there's something wrong in User.rb. As @jaimerump said, it's probably an unmatched end

beisert1 commented 10 years ago

I cant believe I missed it!! you guys are right! I had an extra end in my user.rb file!!!

beisert1 commented 10 years ago

Thanks everyone :+1: