开发者

using different session model name in Authlogic

开发者 https://www.devze.com 2023-02-13 06:49 出处:网络
I changed UserSession model name to Session in Authlogic gem but i\'m getting this error. NoMethodError in SessionsController#new

I changed UserSession model name to Session in Authlogic gem but i'm getting this error.

NoMethodError in SessionsController#new

undefined method `login_field' for Object:Class

Is it forbidden to use Session as a model name ?

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])

      i开发者_开发知识库f @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 %>


Try:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消