ruby-on-rails-3 - 设置 Authlogic 我一直点击 : "undefined method ` login' for #<UserSession: no credentials provided>"

标签 ruby-on-rails-3 authlogic

我正在尝试(未成功)为我的 Rails3 项目设置 Authlogic。在 user_sessions#new View 中,我不断收到以下错误:

undefined method `login' for #<UserSession: no credentials provided>
Extracted source (around line #7):

5:   <%= form_for(:user_session, :url => user_session_path) do |f| %>
6:     <%= f.label :login %><br>
7:     <%= f.text_field :login %><br>
8:     <%= f.password_field(:password) %>   
9:     <%= f.submit %>
10:   <% end %>

我相信出现错误是因为 Authlogic 无法检测到哪个数据库列应用于登录字段。

我的理解是,如果不存在登录列,Authlogic 应该依靠 :email 列。对于腰带和背带,我尝试向 User 类添加配置选项,但仍然没有解决问题

这是我的代码:

用户模型

class User < ActiveRecord::Base

  attr_accessible :password, :password_confirmation
  acts_as_authentic do |c|
    c.login_field = :email
  end
end

UserSessions Controller

class UserSessionsController < ApplicationController  
  def new
    @user_session = UserSession.new
  end

  # ...
end

UserSessions#新 View

<%= form_for(:user_session, :url => user_session_path) do |f| %>
  <%= f.label :login %><br>
  <%= f.text_field :login %><br>
  <%= f.password_field(:password) %>   
  <%= f.submit %>
<% end %>

UserSession模型

class UserSession < Authlogic::Session::Base
   # configuration here, see documentation for sub modules of Authlogic::Session
end

数据库架构(用于 authlogic 表)

create_table "sessions", :force => true do |t|
  t.string   "session_id", :null => false
  t.text     "data"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"

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

为什么 Authlogic 不使用我告诉它的登录列(或者这就是问题所在)?

最佳答案

您告诉 AuthLogic 使用email 字段,但随后在您的 View 中使用login 字段。将您的 View 更改为:

<%= form_for(:user_session, :url => user_session_path) do |f| %>
  <%= f.label :email %><br>
  <%= f.text_field :email %><br>
  <%= f.password_field(:password) %>   
  <%= f.submit %>
<% end %>

它应该可以工作。

关于ruby-on-rails-3 - 设置 Authlogic 我一直点击 : "undefined method ` login' for #<UserSession: no credentials provided>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5573208/

相关文章:

ruby-on-rails - 使用 "autotest"测试通过,但使用 Authlogic 测试未通过 "rake test"

ruby-on-rails - Authlogic:如何仅在密码更新时验证密码?

ruby-on-rails - ruby rails : Authlogic Facebook integration: Link accounts with user input instead of automatically using email address

ruby-on-rails-3 - mongoid 的枚举数据类型

ruby-on-rails - 将类添加到生成的表单的正确 button_to 语法是什么?

ruby-on-rails-3 - 如何使用 formtastic 编写无资源的表单

ruby-on-rails - session 生成器不适用于 Rails 3 中的 Authlogic。帮帮我!

ruby-on-rails - 为什么 form_tag 和 form_for 的用法在 rails(erb) 中发生变化?

jquery - 未选中复选框时的默认值

ruby-on-rails - authlogic openid auto_register 功能尝试重复注册