我正在嘗試使用AuthLogic將簡單的登錄設置到我的用戶表中。每次我嘗試時,登錄失敗,我不知道爲什麼。我確信這是一個簡單的錯誤,但我一直在用它打磚牆。設置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爲我的用戶表生成了一些數據,但我無法登錄!每次我嘗試它只是重定向到索引。我哪裏錯了?謝謝大家。
------ UPDATE ------
我實現的Jakub Hampl與建議的form_for剛纔 - 我得到一個新的錯誤。
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)
我根本沒有改變控制器。感謝所有迴應這個話題的人 - 這對我來說非常有幫助。我現在能做些什麼來克服這個障礙?
------ UPDATE#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?
是否在索引控制器的索引操作中分配了@user_session? – x1a4 2010-05-15 07:40:46