2010-05-15 62 views
2

我正在嘗試使用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?

+0

是否在索引控制器的索引操作中分配了@user_session? – x1a4 2010-05-15 07:40:46

回答

1

一個好主意是使用form_for如果你所能:

<% form_for @user_session do |f| %> 
    <p> 
    <%= f.label :username %> 
    <%= f.text_field :username, :class => :inputBox %> 
    </p> 
    <p> 
    <%= f.label :password %> 
    <%= f.password_field :password, :class => :inputBox %> 
    </p> 
    <p><%= f.submit "submit", :class => :submit %></p> 
<% end %> 

除此之外很難說。你的日誌文件是否提供了任何細節(又名錯誤)?另外,嘗試將表中的錯誤添加到閃存中,以便您可以看到發生了什麼問題。

由於它似乎來自你最後一次更新@user_session沒有設置,只需繼續創建一個:<% form_for UserSession.new do |f| %>

+0

「登錄」與authlogic一起工作良好。 – x1a4 2010-05-15 02:46:28

+0

我的不好,謝謝你的更正。 – 2010-05-15 03:08:57

+0

編輯以適應OP的編輯。 – 2010-05-15 17:25:43

1

在你的情況下,params [:user_session]是空的,因爲它沒有被設置在你的視圖中。我認爲Jakub Hampl建議使用form_for是最好的方法,但您也可以通過將輸入名稱設置爲user_session[login]user_session[password]來保留form_tag,或者您可以將行動中的行更改爲@user_session = UserSession.new(:login => params[:login], :password => params[:password])

+0

這是正確的。您可以通過檢查該POST請求的日誌來確認這一點。它將提交正確的字段,但它們不像控制器中預期的那樣限定在user_session範圍內。 – 2010-05-15 02:52:44