似乎我搞砸了一些东西而且我不确定它是什么!但它不起作用,因为它是假设工作
最初我试图实现一个记住我的盒子,但这需要一个旋转现在如果我编辑客户的详细信息并更新它然后我会自动注销。不知道为什么会发生这种情况,但这里有一个片段
类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
更新::尝试在过滤之前注释掉这里发生了什么。按照你的教程我尝试使用你的方法来确保它不是一个小点或一些点。但那就是我现在的新错误!
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)
#
我不明白为什么这些方法没有定义。他们是自我推荐!!
答案 0 :(得分:0)
嗯,我查看了你的代码,但看不到任何会导致注销的东西。这是我的教程应用程序的链接:https://github.com/htw-rails/TutorialSampleApp32 - 所有方法看起来都很近和你的一样。我会尝试评论before_filters,看看是否有所作为。