无法访问Cells中的current_user属性

时间:2015-04-15 06:09:09

标签: ruby-on-rails cells

我有一个RoR应用程序使用Devise和User表进行用户身份验证。我以下列方式使用这些单元格:

class EnrollmentCell < Cell::Rails

  def show(current_user)
    logger.debug "This is the current user: #{current_user}"
    user = current_user

    logger.debug "This is the ID of the user: #{user.id}"
    logger.debug "This is the ID of the user: #{user.email}"
end 

日志显示正在接收current_user哈希:

Started GET "/" for ::1 at 2015-04-14 23:03:16 -0700
Processing by VisitorsController#index as HTML
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 2]]
  Enrollment Load (0.1ms)  SELECT "enrollments".* FROM "enrollments" WHERE "enrollments"."user_id" = ?  [["user_id", 2]]
This is the current user: {:user=>#<User id: 2, email: "testuser@mac.com", encrypted_password: "$2a$10$QfdzPmD/QqvFUu31Qn43x.u9h6qKiIzdWXtAcAoAK1O...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 4, current_sign_in_at: "2015-04-15 01:25:28", last_sign_in_at: "2015-04-13 16:32:09", current_sign_in_ip: "::1", last_sign_in_ip: "::1", created_at: "2015-04-11 14:29:28", updated_at: "2015-04-15 01:25:28", name: "Steve Smith", confirmation_token: nil, confirmed_at: "2015-04-11 14:29:46", confirmation_sent_at: "2015-04-11 14:29:29", unconfirmed_email: nil, role: 0>}
  Rendered visitors/index.html.erb within layouts/application (39.1ms)
Completed 500 Internal Server Error in 43ms (ActiveRecord: 1.1ms)

NoMethodError - undefined method `id' for #<Hash:0x007fa0c420f960>:
  app/cells/enrollment_cell.rb:7:in `show'

我希望能够访问&#34; id&#34;和&#34;电子邮件&#34; &#34;用户&#34;。我似乎无法弄清楚为什么我无法访问这些信息,但我确信有一些简单的东西我可以忽略这里。

1 个答案:

答案 0 :(得分:1)

如下所示编写此课程:

class EnrollmentCell < Cell::Rails

  def show(current_user)
    logger.debug "This is the current user: #{current_user}"
    user = current_user

    logger.debug "This is the ID of the user: #{user[:user].id}"
    logger.debug "This is the ID of the user: #{user[:user].email}"
  end
end