2010-05-15 3 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 %> 

나는 사용자 테이블에 대해 일부 데이터를 생성했지만 로그인 할 수 없습니다. 시도 할 때마다 색인으로 리디렉션됩니다. 내가 어디로 잘못 가고 있니? 감사합니다 여러분.

------ UPDATE ------

지금 막 form_for와 야쿱 Hampl의 제안을 구현 - 나는 새로운 오류를 받고 있어요.

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) 

나는 컨트롤러를 전혀 변경하지 않았습니다. 이 주제에 대해 답변 해 주신 모든 분들께 감사드립니다. 이 장애물을 극복하기 위해 지금 무엇을 할 수 있습니까?

------

내 응용 프로그램 컨트롤러는 다음과 같습니다.

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으로 변경해야합니까? 그 외에도 하드에서

<% 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 %> 

말 :

+0

@user_session이 인덱스 컨트롤러의'index' 액션에 할당되어 있습니까? – x1a4

답변

1

좋은 아이디어는 당신이 가능하게 할 수있는 경우에 form_for을 사용하는 것입니다. 로그 파일에 세부 정보 (일명 오류)가 있습니까? 또한 테이블에있는 오류를 플래시에 추가하여 잘못된 점을 볼 수 있도록하십시오.

@user_session이 설정되지 않은 마지막 업데이트로 보입니다. 따라서 <% form_for UserSession.new do |f| %>을 생성하십시오.

+0

'로그인'은 authlogic에서도 잘 작동합니다. – x1a4

+0

내 잘못, 고마워 고마워. –

+0

OP 편집을 편집했습니다. –

1

귀하의 경우 params [: user_session]이 (가)보기에 설정되어 있지 않으므로 비어 있습니다. Jakub Hampl이 form_for를 사용하는 것이 가장 좋은 방법이라고 생각하지만, 입력 이름을 user_session[login]user_session[password]으로 설정하여 form_tag를 유지하거나 동작의 행을 으로 변경할 수 있습니다.

+0

이것은 맞습니다. 해당 POST 요청에 대한 로그를 검사하여이를 확인할 수 있습니다. 올바른 필드가 전송되지만 컨트롤러에서 예상되는 것처럼 user_session에 범위가 지정되지 않습니다. –