【发布时间】:2011-02-19 18:54:00
【问题描述】:
我正在尝试使用 AuthLogic 在我的用户表中设置一个简单的登录。每次我尝试,登录失败,我不知道为什么。我确定这是一个简单的错误,但我已经用它碰了壁。
#user_sessions_controller
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
else
flash[:notice] = "We couldn't log you in. Please try again!"
redirect_to :controller => "index", :action => "index"
end
end
#_user_login.html.erb (this is the partial from my index page where Users log in)
<% form_tag user_session_path do %>
<p><label for="login">Login:</label>
<%= text_field_tag "login", nil, :class => "inputBox", :id => "login",
</p>
<p><label for="password">Password: </label>
<%= password_field_tag "password", nil, :class => "inputBox", :id => "password",
</p>
<p><%= submit_tag "submit", :class => "submit" %></p>
<% end %>
我让 Faker 为我的用户表生成了一些数据,但我无法登录!每次我尝试它只是重定向到索引。我哪里错了?谢谢大家。
-----更新------
我刚才用 form_for 实现了 Jakub Hammpl 的建议 - 我遇到了一个新错误。
ActionView::TemplateError (called id for nil, which would mistakenly be 4 -
1: <% form_for @user_session do |f| %>
2: <% if flash[:notice] -%>
3: <p class="notice"><%= flash[:notice] %></p>
4: <% end -%>
app/views/index/_user_login.html.erb:1
app/views/layouts/index.html.erb:65
app/controllers/index_controller.rb:3:in `index'
Rendered rescues/_trace (86.0ms)
Rendered rescues/_request_and_response (1.0ms)
Rendering rescues/layout (internal_server_error)
我根本没有改变控制器。感谢所有回复这个话题的人——这对我非常有帮助。我现在可以做些什么来克服这个障碍?
-----更新#2-----
这是我的应用程序控制器。
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to login_path
return false
end
end
def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to root_url
return false
end
end
其中哪一项应更改为@user_session?
【问题讨论】:
-
是否在索引控制器的
index操作中分配了@user_session?
标签: ruby-on-rails session login authlogic