0

Authlogic gem をインストールし、コントローラーとモデルをセットアップしました。Authlogic に新しいアカウントを登録すると、DB テーブルにすべてが正常に作成されます ( 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

user_session.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

user.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

2 日目で既にこの問題を解決しようとしていますが、まだ問題を見つけることができません... DB テーブルには、Authlogic に必要なすべてのデータが作成されていますが、ログインしようとすると、常にエラー メッセージWrong credentials が表示されます。.

この問題について誰か助けてくれませんか?私はすでにそれを修正する方法がわかりません:/

4

1 に答える 1

0

わかりました、問題は解決したようです - 私は自分の目的のためにAuthlogic も使用する列名activeを使用しました。アクティビティに名前を変更し、すべてがうまく機能しています。

于 2012-09-30T11:47:29.253 に答える