0

我安装了 Authlogic gem,设置了控制器和模型。当我使用 Authlogic 注册新帐户时,数据库表中的所有内容都已成功创建(如crypted_pa​​sswordpassword_salt等)。组件:UserSessionsController

# encoding: utf-8
class UserSessionsController < ApplicationController
  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])

    respond_to do |format|
      if @user_session.save
        format.html { redirect_to(root_url, :flash => { success: 'Successfully logged in.'}) }
        format.xml  { render :xml => @user_session, :status => :created, :location => @user_session }
      else
        format.html { #render :action => :new
                      redirect_to :back
                      flash[:warning] = 'Wrong credentials.' 
                    }
        format.xml  { render :xml => @user_session.errors, :status => :unprocessable_entity }         
      end
    end
  end 

  def signin
    @user_session = UserSession.new
  end

  def destroy
    @user_session = UserSession.find
    @user_session.destroy

    respond_to do |format|
      format.html { redirect_to(root_url, :notice => 'Logged out.') }
      format.xml  { head :ok }
    end
  end
end

用户控制器

class UsersController < ApplicationController
  filter_access_to :all

  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end

  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user }
    end
  end

  def signup
    @user = User.new
    @categories = Category.order('name')
  end

  def new
    @user = User.new
    @categories = Category.order('name')

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end

  def edit
    @user = User.find(params[:id])
    @categories = Category.order('name')
    redirect_to root_url unless current_user.id == @user.id
  end

  def create
    params[:user][:id_code] = User.random_numbers
    params[:user][:url_name] = params[:user][:name].parameterize
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        Assignment.create(:user_id => @user.id, :role_id => MEMBER)
        format.html { redirect_to root_url, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end


  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end

end

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :mailer_set_url_options
  before_filter :set_current_user
  helper_method :current_user_session, :current_user

  def mailer_set_url_options
    ActionMailer::Base.default_url_options[:host] = request.host_with_port
  end

  protected
     def set_current_user
       Authorization.current_user = current_user
     end

   private
     def current_user_session
       logger.debug "ApplicationController::current_user_session"
       return @current_user_session if defined?(@current_user_session)
       @current_user_session = UserSession.find
     end

     def current_user
       logger.debug "ApplicationController::current_user"
       return @current_user if defined?(@current_user)
       @current_user = current_user_session && current_user_session.user
     end

     def authentication
       logger.debug "ApplicationController::authentication"
       unless current_user
         #store_location
         flash[:warning] = "You must be logged out to access this page"
         redirect_to root_url
         return false
       end
     end
end

用户会话.rb

class UserSession < Authlogic::Session::Base
#  attr_accessible :data, :sessions_id
  generalize_credentials_error_messages "Login info is invalid!"  

  def to_key
     new_record? ? nil : [ self.send(self.class.primary_key) ]
  end

  self.logout_on_timeout = true  

  def persisted?
    false
  end
end

用户.rb

class User < ActiveRecord::Base
  attr_accessible #list of all columns

  acts_as_authentic do |c|
    c.login_field = 'email'
    c.logged_in_timeout(15.minutes)
  end

  has_many :assignments
  has_many :roles, :through => :assignments

  #validates...

  # declarative_authentications
  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end
end

我已经在第二天尝试解决此问题,但我仍然找不到问题...在数据库表中创建了 Authlogic 所需的所有数据,但是当我尝试登录时,我总是收到错误消息错误的凭据。.

有人可以帮我解决这个问题吗?我已经不知道如何解决它:/

4

1 回答 1

0

好的,问题似乎已经解决了——我出于我的目的使用了列名active,它也使用了 Authlogic。重命名为活动,一切正常。

于 2012-09-30T11:47:29.253 回答