我正在尝试(未成功)为我的 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/