ruby on rails - User sessions and logins -
i trying implement signup & login features on ror. on index page have created 2 links saying 'new user' , 'login' , in userscontroller have signup & login methods(updated routes accordingly).
prob: upon clicking new user or login getting error saying
"the action 'show' not found userscontroller"
routes.rb:
rails.application.routes.draw resources :users 'users/register', :to=>'users#register' 'users/signup', :to=>'users#signup' post 'users/signup', :to=>'users#signup' post 'users/login', :to=>'users#login' 'users/login', :to=>'users#login' post "users/change_password" => "users#change_password" "users/change_password" => "users#change_password"
index.html.erb
<%= link_to "new user", users_register_path %><br> <%= link_to "login", users_login_path %><br> <%= link_to "change password", users_change_password_path %><br>
userscontroller:
def index @user_details = user.all end def register puts "**********************" puts params @new_user = user.new end def signup @new_user = user.new(user_register) if @new_user.save session[:user] = user.authenticate(@user.name,@user.password) redirect_to :action=>"welcome" else redirect_to :action=>"login" end end def login puts params if request.post? session[:user] = user.authenticate(params[:user][:name], params[:user][:password]) redirect_to :action=>"welcome" else # redirect_to :action=>"signup" end end def change_password puts "**********************" puts params puts "**********************" if request.post? @pass_change = user.new_password(params[:user][:name], params[:user][:password], params[:user][:new_password]) end end def welcome end def user_register params.require(:user).permit(:name,:email,:password,:password_confirmation) end end
usermodel.rb:
require 'digest/sha1' class user < activerecord::base attr_accessor :password, :password_confirmation def password=(pass) @password = pass self.salt = user.random_met(10) self.hashedpassword = user.encrypt(@password, self.salt) end def self.encrypt(pass,salt) digest::sha1.hexdigest(pass+salt) end def self.authenticate(login, pass) user_auth = user.find(:first, :conditions=>["login = ?", login]) return nil if user_auth.nil? return user_auth if user.encrypt(pass,user_auth.salt)==user_auth.hashedpassword end def self.new_password(name,old,new) user = where(:name=>name) #puts user.email # how call setter(password) method here. because idea #if user.encrypt(old, user.salt)==user.hashedpassword # run def password=(pass), thereby storing new password. end def self.random_met(len) char = ("a".."z").to_a + ("a".."z").to_a + ("0".."9").to_a salted = "" 1.upto(len) { |i| salted << char[rand(char.size-1)] } return salted end end
as written in guides,
rails routes matched in order specified, if have resources :photos above 'photos/poll' show action's route resources line matched before line. fix this, move line above resources line matched first.
you have defined resources :users
above get routes
, rails show route
, error.
Comments
Post a Comment