在Authlogic中使用不同的会话模型名称

时间:2011-02-25 20:29:33

标签: ruby-on-rails authlogic

我在Authlogic gem中将UserSession模型名称更改为Session但我收到此错误。

NoMethodError in SessionsController#new

undefined method `login_field' for Object:Class

是否禁止将Session用作型号名称?

db schema

 create_table "users", :force => true do |t|
    t.string   "login"
    t.string   "email"
    t.string   "crypted_password"
    t.string   "password_salt"
    t.string   "persistence_token"
    t.string   "perishable_token"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

sessions_controller

class SessionsController < ApplicationController

  def new
    @session = Session.new
  end

  def create
    @session = Session.new(params[:session])

      if @session.save
        redirect_to(root_url, :notice => 'Login successful.')
      else
        render :action => "new"

      end

  end

  def destroy
    @session = Session.find
    @session.destroy
    redirect_to(root_url, :notice => 'Goodbye!')
  end
end

model:session.rb

class Session < Authlogic::Session::Base

end

application_controller

class ApplicationController < ActionController::Base
  protect_from_forgery


 helper_method :current_user_session, :current_user

    private
      def current_user_session
        return @current_user_session if defined?(@current_user_session)
        @current_user_session = Session.find
      end

      def current_user
        return @current_user if defined?(@current_user)
        @current_user = current_user_session && current_user_session.user
      end


end

_form.html.erb

<%= form_for(@session) do |f| %>

  <% if @session.errors.any? %>
    <div class="error messages">
      <ul>
      <% @session.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :login %>
    <%= f.text_field :login %>
  </div>
  <div class="field">
    <%= f.label :password %>
    <%= f.password_field :password %>
  </div>
  <div class="button">
    <%= f.submit "Submit" %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:3)

尝试:

class Session < Authlogic::Session::Base
  authenticate_with User
end


class User < ActiveRecord::Base
  acts_as_authentic do |configuration| 
    configuration.session_class = Session
  end
end