代码之家  ›  专栏  ›  技术社区  ›  San Diago

遵循Authlogic教程,但是#<UserSession:{:未授权的#记录=>“<protected>”}>

  •  0
  • San Diago  · 技术社区  · 15 年前

    official Authlogic tutorial 但是出了点事。

    我成功地创建了一个用户,但是当我尝试 show

    undefined method `email' for #<UserSession: {:unauthorized_record=>"<protected>"}>
    

    以下是相关代码:

    # user.rb
    class User < ActiveRecord::Base  
      acts_as_authentic do |c|
        c.crypto_provider = Authlogic::CryptoProviders::BCrypt
        c.require_password_confirmation(false)
      end  
      attr_accessible :username, :email, :password  
    end
    
    # users_controller.rb    
    def show
      @user = @current_user
    end
    
    # application_controller.rb
    private
      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 and current_user_session.user
      end
    

    # views/users/show.html.erb
    <%=h @user.username%>
    email: <%=h @user.email %>
    <%= link_to "Edit", edit_account_path %>
    

    当我删除“email”行时,我得到一个只有“Edit”链接的页面。用户名不出现。但是,当我加上 <%= debug @user %> 要查看发生了什么,它会正确地打印 email username @user.username ,以及为什么在我尝试打印时它会抛出错误 @user.email .

    应当指出的是 show.html.erb layout of the tutorial 使用 @user.login ,而且很有效。我刚把“登录”改成“用户名”。

    此外,当我单击“编辑”时,我会得到相同的原始错误:

    这是你的答案 edit

    # views/users/edit.html.erb
    <h1>Edit My Account</h1>
    
    <% form_for @user, :url => account_path do |f| %>
      <%= f.error_messages %>
      <%= render :partial => "form", :object => f %>
      <%= f.submit "Update" %>
    <% end %>
    <br />
    <%= link_to "My Profile", account_path %>
    
    #views/users/_form.erb
    <%= form.label :username %><br />
    <%= form.text_field :username %><br />
    <br />
    <%= form.label :email %><br />
    <%= form.text_field :email %><br />
    <br />
    <%= form.label :password, form.object.new_record? ? nil : "Change password" %><br />
    <%= form.password_field :password %><br />
    <br />
    

    我正在运行Rails2.3.2和最新的Authlogic(今天刚刚安装,不管是什么版本),就我疲惫和沮丧的大脑所知,我的数据库中确实有所有必需的字段,而且 they are spelled correctly .

    发生什么事?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Community CDub    7 年前

    好吧,这太尴尬了

    原来问题是我对自然语言的偏爱。这是你的答案 current_user 本教程中列出的方法:

    def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
    end
    

    && 对于 and ,和 as a result current_user_session.user UserSession 这个和那个。我觉得这很奇怪,但看不出是从哪里来的。现在我知道了。

        2
  •  1
  •   vrybas    13 年前

    现在你可以替换

    UserSession.create u
    

    UserSession.create :email => u.email, :password => u.password
    

    没什么大不了的。

    但我被另一种方式吸引住了。我忘了我的用户处于活动状态?==创建时为false。我已将其设置为true并创建会话。

        3
  •  0
  •   adimitri    15 年前

    # users_controller.rb    
    def show
      @user = current_user
    end
    

    请注意,current\u user实际上是一个方法,而不是实例变量@current\u user。