Невозможно получить доступ к атрибутам current_user в ячейках

У меня есть приложение 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'

Я ожидал, что смогу получить доступ к "id" и "email" пользователя. Я не могу понять, почему я не могу получить доступ к этой информации, но я уверен, что есть кое-что простое, что я пропускаю здесь.

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 
Другие вопросы по тегам