Reputation: 41
I am using cells 4 to render a login form in rails 4, and I get the following error:
'undefined local variable or method `current_user' for <PopupLoginCell:0xbbd32e74>'
I have tried including include ApplicationHelper
.
Please help me.
#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
Upvotes: 0
Views: 366
Reputation: 4450
You can pass current_user to cell as an option
#haml/slim
= cell(PopupLoginCell, my_model, current_user: current_user)
#erb
<%= cell(PopupLoginCell, my_model, current_user: current_user) %>
Upvotes: 1
Reputation: 1021
In you cell class you must include the Devise helpers:
class PopupLoginCell < Cell::ViewModel
include Devise::Controllers::Helpers
helper_method :current_user
# Your class code
end
I hope it helps.
Upvotes: 1