开发者

Where I got a NoMethodError using restful authorization?

This is the code from my login Page:

<%= flash[:notice]%>
<h1>System</h1>             
<%= Time.now%></p>
<p><%=link_to "Register", signup_path%></p>  
<div id="login">
    <%if logged_in? %>       
        <b><%=current_user.login%></b>  
        <%=link_to "Signout", logout_path%>
    <%else%>
        <% form_remote_tag :url => session_path do -%>
        <p><%= label_tag 'login' %>
        <%= text_field_tag 'login', @login %> <%= label_tag 'password' %>
        <%= password_field_tag 'password', nil %></p>

        <p><%= label_tag 'remember_me', 'Remember me' %>
        <%= check_box_tag 'remember_me', '1', @remember_me %></p>
        <p><%= submit_tag 'Log in' %></p>
        <% end -%>        
    <%end%>  
</div>     

And this is the sessions_controller.rb:

# This controller handles the login/logout function of the site.  
class SessionsController < ApplicationController
  # Be sure to include AuthenticationSystem in Application Controller instead
  #include AuthenticatedSystem

  # render new.rhtml
  def new
  end

  def create
    logout_keeping_session!
    user = User.authenticate(params[:login], params[:password])
    if user
      # Protects against session fixation attacks, causes request forgery
      # protection if user resubmits an earlier form using back
      # button. Uncomment if you understand the tradeoffs.
      # reset_session
      self.current_user = user
      new_cookie_flag = (params[:remember_me] == "1")
      handle_remember_cookie! new_cookie_flag 
      #redirect_back_or_default('/')
      flash[:notice] = "Logged in successfully"  
      self.reload_login() 
    else
      note_failed_signin
      @login       = params[:login]
      @remember_me = params[:remember_me]
      render :action => 'new'
    end
  end

  def destroy
    logout_killing_session!
    flash[:notice] = "You have been logged out."
    redire开发者_如何学运维ct_back_or_default('/')
  end     

  def reload_login   
    respond_to {|format| format.js}
    flash[:notice] = "What the hell?" 

  end

protected   
  # Track failed login attempts
  def note_failed_signin
    flash[:error] = "Couldn't log you in as '#{params[:login]}'"
    logger.warn "Failed login for '#{params[:login]}' from #{request.remote_ip} at #{Time.now.utc}"
  end
end

I can login the page, but while I wait for a while, several minutes later, I found that I got NoMethodError in System#index error...

undefined method `remember_token?' for true:TrueClass
Extracted source (around line #6):

3: <%= Time.now%></p>
4: <p><%=link_to "Register", signup_path%></p>  
5: <div id="login">
6:  <%if logged_in? %>       
7:      <b><%=current_user.login%></b>  
8:      <%=link_to "Signout", logout_path%>
9:  <%else%>

I don't know what's going wrong, it works in the first launch. But if I go to "http://localhost:3000/login" to login again, the error is gone, what happened? thank u.


Open authenticated_system.rb and change and to && in the following line( it is 128 on me):

user = !cookies[:auth_token].blank? and User.find_by_remember_token(cookies[:auth_token])

It is a common mistake to interchange and to && :

p true and false # returns true
p true && false # returns false, this is the conventional behaviour


That's a controller method. The restful auth plugin provides access to the view with the helper_method call.

http://bs.techno-weenie.net/!source/2848/plugins/restful_authentication/generators/authenticated/templates/authenticated_system.rb#96


have you looked into the database migration. Please check if you have the attribute remember_token in your table and whether its type is bool or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜