无法访问单元格中的 current_user 个属性
Unable to access current_user attributes in Cells
我有一个 RoR 应用程序,使用 Devise 和用户 table 进行用户身份验证。我按以下方式使用单元格:
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: "a$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'
我本来希望能够访问 "user" 的 "id" 和 "email"。我似乎无法弄清楚为什么我无法访问此信息,但我确信我在这里忽略了一些简单的事情。
这样写 class 如下所示:
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
我有一个 RoR 应用程序,使用 Devise 和用户 table 进行用户身份验证。我按以下方式使用单元格:
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: "a$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'
我本来希望能够访问 "user" 的 "id" 和 "email"。我似乎无法弄清楚为什么我无法访问此信息,但我确信我在这里忽略了一些简单的事情。
这样写 class 如下所示:
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