1
我正試圖在ROR上實現註冊&登錄功能。在索引頁上,我創建了2個鏈接,說'新用戶'和'登錄',並在用戶控制器我有註冊&登錄方法(相應更新的路線)。用戶會話和登錄
概率:在點擊新用戶或登錄我得到一個錯誤說
「動作 '顯示' 無法找到UsersController」
的routes.rb:
Rails.application.routes.draw do
resources :users
get 'users/register', :to=>'users#register'
get 'users/signup', :to=>'users#signup'
post 'users/signup', :to=>'users#signup'
post 'users/login', :to=>'users#login'
get 'users/login', :to=>'users#login'
post "users/change_password" => "users#change_password"
get "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>
用戶控制器:
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 to call setter(password) method from here. because My idea is
#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
點擊哪個'link'產生'error'? – Pavan
所有鏈接。新用戶,登錄 – user3484925
嘗試刪除頂部的'resources:users'並將其添加到您的'routes.rb'中的最後一個 – Pavan