我使用restful授权获得NoMethodError的地方?

时间:2010-07-09 10:02:58

标签: ruby-on-rails ruby-on-rails-plugins

这是我登录页面的代码:

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

这是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."
    redirect_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

我可以登录页面,但是等了一会儿,几分钟后,我发现我在System#index错误中得到NoM​​ethodError ...

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

我不知道出了什么问题,它在首次发布时有效。但如果我再次登录“http://localhost:3000/login”,错误就会消失,发生了什么?谢谢你。

3 个答案:

答案 0 :(得分:4)

打开authenticated_system.rb并将and更改为以下行中的&&(对我来说是128):

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

&amp;&amp; 互换是一个常见的错误:

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

答案 1 :(得分:0)

这是一种控制器方法。 restful auth插件提供访问权限 使用helper_method调用到视图。

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

答案 2 :(得分:0)

您是否考虑过数据库迁移。请检查表中是否包含remember_token属性以及其类型是否为bool。