2012-07-23 176 views
0

看來我搞砸了一些東西,我不確定它是什麼!但它不工作,因爲它是假設工作Ruby on Rails登錄會話

最初我試圖實現一個記住我框,但這花了一個旋轉,現在如果我編輯客戶的詳細信息和更新它,然後我會自動註銷。不知道爲什麼會這樣,但這裏的一個片段

類CustomersController < ApplicationController的

def index 
    @customers = Customer.all 
end 

def new 
    @customer = Customer.new 
end 

def show 
    @customer = Customer.find(params[:id]) 
    @posts = @customer.posts 
end 

def create 
     @customer = Customer.new(params[:customer]) 
     if @customer.save 
     sign_in @customer 
     flash[:success] = "Welcome to Where you Where!" 
     redirect_to @customer 

     else 
      render 'new' 
     end 
end 

def edit 
    @customer = Customer.find(params[:id]) 
end 

def update 
    if @customer.update_attributes(params[:customer]) 
     flash[:success] = "Profile Updated" 
     redirect_to @customer 
    else 
     render 'edit' 
    end 
end 

def destroy 
    Customer.find(params[:id]).destroy 
    redirect_to root_path 
end 

private 

    def current_customer?(customer) 
     customer == current_customer 
    end 

    def correct_customer 
     @customer = Customer.find(params[:id]) 
     redirect_to(root_path) unless current_customer?(@customer) 
    end 

    def admin_customer 
     redirect_to(root_path) unless current_customer && current_customer.admin? 
    end 

這裏我的會話控制器

module SessionsHelper 

def sign_in(customer) 
    cookies.permanent.signed[:remember_token] = [customer.id, customer.salt] 
    self.current_customer = customer 
end 
def sign_out 
    cookies.delete(:remember_token) 
    self.current_customer = nil 
    end 

    def signed_in? 
    !current_customer.nil? 
    end 

def current_customer?(customer) 
    return false unless current_customer 
    current_customer.id == customer.id 
    end 
    def current_customer=(customer) 
    @current_customer = customer 
    end 

    def current_customer 
    @current_customer ||= customer_from_remember_token 
    end 
    def authenticate 
    deny_access unless signed_in? 
    end 
    def deny_access 
    store_location 
    redirect_to signin_path, :notice => "Please sign in to access this page." 
    end 
    def redirect_back_or(default) 
    redirect_to(session[:return_to] || default) 
    clear_return_to 
    end 


    private 

    def customer_from_remember_token 
     Customer.authenticate_with_salt(*remember_token) 
    end 

    def remember_token 
     cookies.signed[:remember_token] || [nil, nil] 
    end 
    def store_location 
    session[:return_to] = request.fullpath 
    end 
    def clear_return_to 
    session[:return_to] = nil 
    end 

end 

這裏的幫助文件

module SessionsHelper 

    def sign_in(customer) 
      cookies.permanent[:remember_token] = customer.remember_token 
      self.current_customer = customer 
    end 

    def signed_in? 
     !current_customer.nil? 
    end 

    def sign_out 
     self.current_customer = nil 
     cookies.delete(:remember_token) 
    end 

    def current_customer=(customer) 
     @current_customer = customer 
    end 

    def current_customer 
     @current_customer ||= Customer.find_by_remember_token(cookies[:remember_token]) 
    end 

    def current_customer?(customer) 
     customer == current_customer 
    end 

    def redirect_back_or(default) 
     redirect_to(session[:return_to] || default) 
     session.delete(:return_to) 
    end 

    def store_location 
     session[:return_to] = request.fullpath 
    end 
end 

我按照在http://ruby.railstutorial.org/chapters/找到的教程,並在第10章。但我也嘗試通過railcast實現一個記住我的複選標記框,這似乎根本不起作用。 (不同的代碼)

再次感謝

我加入這個額外的支持

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    before_filter :pages 

    def pages 
     @pages = Page.all 
    end 

private 

    def current_customer 
     @current_customer ||= Customer.find(session[:customer_id]) if session[:customer_id] 
    end 
    helper_method :current_customer 

    def authorize 
     redirect_to login_url, alert: "Not authorized" if current_customer.nil? 
    end 

    include SessionsHelper 
end 

更新::試圖過濾這裏發生了什麼之前,註釋掉後。遵循你的教程,我嘗試使用你的方法來實現它,以確保它不是一個小點或somesort。但那是我現在的新錯誤!

NameError in SessionsController#create 
undefined local variable or method `encrypted_password' for #<Customer:0xb57f11c8> 

所以在這裏我最新的客戶

class Customer < ActiveRecord::Base 
# RELATIONS 

    has_many :posts, dependent: :destroy 

# Data Access 

    attr_accessor :password 
    attr_accessible :first_name, :last_name, :middle_name, :email, :password, :password_confirmation 
    before_save :encrypt_password 

# VALIDATION 

    validates :first_name, presence: true, length: { maximum: 50 } 
    validates :middle_name, length: { maximum: 50 } 
    validates :last_name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, 
        format:  { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
    validates_uniqueness_of :email 
    validates :password, presence: true, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 

# METHODS 

    def has_password?(submitted_password) 
     encrypted_password == encrypt(submitted_password) 
    end 

    def self.authenticate(email, submitted_password) 
     customer = find_by_email(email) 
     customer && customer.has_password?(submitted_password) ? customer : nil 
    end 

    def self.authenticate_with_salt(id, cookie_salt) 
     customer = find_by_id(id) 
     (customer && customer.salt == cookie_salt) ? customer : nil 
    end 

private 
    def encrypt_password 
     self.salt = make_salt if new_record? 
     self.encrypted_password = encrypt(password) 
    end 
    def encrypt(string) 
     secure_hash("#{salt}--#{string}") 
    end 
    def make_salt 
     secure_hash("#{Time.now.utc}--#{password}") 
    end 
    def secure_hash(string) 
     Digest::SHA2.hexdigest(string) 
    end 
end 


# == Schema Information 
# 
# Table name: customers 
# 
# id    :integer   not null, primary key 
# first_name  :string(255) 
# email   :string(255) 
# created_at  :datetime  not null 
# updated_at  :datetime  not null 
# password_digest :string(255) 
# remember_token :string(255) 
# last_name  :string(255) 
# middle_name  :string(255) 
# auth_token  :string(255) 
# login_count  :integer   default(0) 
# current_login_at :datetime 
# last_login_at :datetime 
# current_login_ip :string(255) 
# last_login_ip :string(255) 
# password_hash :string(255) 
# password_salt :string(255) 
# 

的模式,我不明白爲什麼這些方法都不能確定。他們是自我引用!

+1

如果你正在使用git,你能提供一個差異到它工作的最後一個版本嗎? – bento 2012-07-23 19:56:51

+0

我沒有使用git,:我確實有一些文件備份,但我更好奇爲什麼我已經註銷我更新了內容。我無法看到我的錯誤他的地方,我沒有看到編輯customerController中的重定向,但如果它的行爲意味着我做了一些混亂,我不知道它在哪裏 – Jseb 2012-07-23 20:00:13

+0

什麼是你的CustomersController'Update'方法看起來像?這不是說你的行動發生在哪裏? – Trip 2012-07-23 21:02:04

回答

0

嗯,我查看了你的代碼,但看不到任何會導致註銷的東西。這裏是我的教程應用程序的鏈接:https://github.com/htw-rails/TutorialSampleApp32 - 所有方法看起來都差不多。與你的一樣。我會嘗試評論before_filters,看看是否有所作爲。

+0

我嘗試它沒有工作,但我沒有隻是一個註銷,但也登錄英寸無論如何看,玩,現在我更新了我的代碼,但我得到一個「不定義encrypted_pa​​ssword」。我錯過了什麼? – Jseb 2012-07-24 04:05:28