0

我正在使用单元格 4 在 rails 4 中呈现登录表单,但出现以下错误:

'undefined local variable or method `current_user' for <PopupLoginCell:0xbbd32e74>'

我试过包括include ApplicationHelper. 请帮我。

#application_controller.rb
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  #protect_from_forgery

  private
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user
end


# show.haml
- unless user_signed_in?
  .popup-login
    = form_for(@user, url:user_session_path) do |f|
      %dl
        %dt= f.label :email
        %dd= f.email_field :email
        %dt= f.label :password
        %dd= f.password_field :password
      = f.submit 'Login'
   :javascript
     $('.header__login-button a').click(function(){
       $('.popup-login').css({ 'display' : 'block'});
     });
#pop_up_login_cell.rb
class PopupLoginCell < Cell::ViewModel
  # helper tự định nghĩa
  include ApplicationHelper


  def show
    @user = User.new unless current_user.present?
    render
  end

end
4

2 回答 2

1

在您的单元格类中,您必须包含设计助手:

class PopupLoginCell < Cell::ViewModel
  include Devise::Controllers::Helpers
  helper_method :current_user

  # Your class code
end

我希望它有所帮助。

于 2015-11-11T10:19:10.067 回答
1

您可以将 current_user 作为选项传递给单元格

#haml/slim
= cell(PopupLoginCell, my_model, current_user: current_user)
#erb
<%= cell(PopupLoginCell, my_model, current_user: current_user) %>

来自官方文档

于 2017-04-28T23:15:00.060 回答