2015-06-13 233 views
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 
+0

點擊哪個'link'產生'error'? – Pavan

+0

所有鏈接。新用戶,登錄 – user3484925

+0

嘗試刪除頂部的'resources:users'並將其添加到您的'routes.rb'中的最後一個 – Pavan

回答

0

正如寫在Guides

Rails的路線在他們指定的順序進行匹配,因此,如果您 有一個資源:以上照片獲取'照片/投票'顯示動作的 路線的資源線將在獲取線之前進行匹配。到 解決這個問題,將獲取線移動到資源行上方,以便首先匹配 。

您已經定義resources :usersget routes以上,所以軌道將尋找一個show route,所以是錯誤