I'm having issues trying to figure out why i'm getting undefined method
current_user?'`
In my Application controller:
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
# Force signout to prevent CSRF attacks
def handle_unverified_request
sign_out
super
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
def authorize
redirect_to login_url, alert: "Not Autherized" if current_user.nil?
end
def track_activity(trackable, action = params[:action])
current_user.activities.create! action: action, trackable: trackable
end
end
In my session helper:
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
end
I'm still new to rails I'm following some tutorials, but I'm trying to figure out why I'm getting undefined method for current_user?
This is where my error is occurring- users view folder:
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
You have not defined a current_user?
method anywhere, and you are calling current_user?(@user)
.
Perhaps, you need to call signed_in?
instead of current_user?
, or define what you mean by current_user?(@user)
You have not defined current_user?
method. there is a difference between current_user
and current_user?
current_user?
always return a boolean response.
but you can try with current_user(@user)?
or unless current_user(@user).nil?
Try this:
def current_user?(user)
user == current_user
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.