2012-07-23 2 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 장에있었습니다.하지만 레일 캐스트로 체크 박스를 기억하려고 시도했는데 전혀 작동하지 않는 것 같습니다. (다른 코드)

다시 한번 감사

여기 필터 무엇 일어나는 전에 주석을 시도 후 :

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으로 작업하는 경우 작동했던 마지막 버전으로 diff를 제공 할 수 있습니까? – bento

+0

나는 자식을 사용하고 있지 않다. : 나는 파일 백업을하고 있지만, 로그 아웃 한 이유에 대해 궁금해한다. 어디서 실수했는지 알 수는 없지만 edit customerController에서 리디렉션이 표시되지 않습니다.하지만 작동하는 경우 무언가 엉망이되어 버렸고 그 위치가 확실하지 않습니다. – Jseb

+0

CustomersController'Update' 메소드는 어떻게 생겼습니까? 당신을 로깅하는 행동이 일어나는 곳이 아닙니까? – Trip

답변

0

흠, 코드를 살펴 봤지만 로그 아웃의 원인이되는 내용을 볼 수 없습니다. 내 튜토리얼 앱에 대한 링크는 다음과 같습니다. https://github.com/htw-rails/TutorialSampleApp32 - 모든 메소드가 약로 보입니다. 너와 같은거야. before_filters를 주석 처리하여 차이가 있는지 확인하려고합니다.

+0

나는 그것을 작동시키지 않으려 고하지만 로그 아웃뿐만 아니라 로그인도하고있다. 어쨌든 내가보고 놀고, 이제는 코드를 업데이트했지만 "encrypted_password를 정의하지 않았다"는 것을 알게되었다. 내가 뭐 놓친 거 없니? – Jseb