似乎我搞砸了一些东西,我不确定它是什么!但它不工作,因为它应该工作
最初我试图实现一个记住我的框,但它需要一个旋转,现在如果我编辑客户详细信息并更新它,那么我会自动注销。不知道为什么会这样,但这里有一个片段
类客户控制器 < 应用控制器
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
更新:: 在过滤之前尝试注释掉这里发生了什么。按照您的教程,我尝试使用您的方法来实现,以确保它不是一个小点或某种东西。但这就是我现在的新错误!
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+\-.]+@[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)
#
我不明白为什么这些方法没有定义。他们是自指的!!