【问题标题】:Following Authlogic tutorial but: undefined method `email' for #<UserSession: {:unauthorized_record=>"<protected>"}>遵循 Authlogic 教程但是:#<UserSession: {:unauthorized_record=>"<protected>"}> 的未定义方法 `email'
【发布时间】:2010-02-11 07:27:10
【问题描述】:

我关注了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 %>

当我删除“电子邮件”行时,我得到一个只有“编辑”链接的页面。用户名不出现。但是,当我添加 &lt;%= debug @user %&gt; 以查看发生了什么时,它会正确打印存储在数据库中的 emailusername 的值。我不知道为什么它不打印@user.username,以及为什么当我尝试打印@user.email 时它会抛出错误。

需要注意的是show.html.erblayout of the tutorial使用@user.login,它可以工作。我只是将“登录”更改为“用户名”。

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

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

这是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 />

再一次,所有内容都是从教程中逐字记录的,这很有效。

我正在运行 Rails 2.3.2 和最新的 Authlogic(今天才安装,无论是什么版本),据我疲惫和沮丧的大脑所知,我确实在数据库中有所有必填字段,并且 @ 987654323@.

发生了什么事?

【问题讨论】:

    标签: ruby-on-rails authentication forms session authlogic


    【解决方案1】:

    嗯,这很尴尬……

    原来问题在于我对自然语言的偏爱。这是教程中介绍的current_user 方法:

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

    但是,在将其应用于我的案例时,我将&amp;&amp; 换成了and,而as a result current_user_session.user 没有被考虑在内。这就是为什么它一直说UserSession 这个那个。我觉得这很奇怪,但看不出它是从哪里来的。现在我知道了。

    【讨论】:

      【解决方案2】:

      现在你可以替换

      UserSession.create u
      

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

      没什么大不了的。

      但这让我很不爽。我忘了我的用户是活跃的? == 创建时为假。我已将其设置为 true 并创建会话。

      【讨论】:

        【解决方案3】:

        我认为您希望使用以下代码:

        # users_controller.rb    
        def show
          @user = current_user
        end
        

        注意 current_user 实际上是一个方法而不是一个实例变量@current_user。

        【讨论】:

        • 方法current_user返回@current_user。但我还是尝试了,没有任何改变。不过谢谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-04
        • 1970-01-01
        • 1970-01-01
        • 2022-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多