So I don't apply an authenticate filter to my index action, but non-logged in users are still unable to access the index view because of this line: :class => "up_arrow #{current_user.votes_for(video).value == 1 ? 'voted' : 'unvoted' }"
which determines whether or not a button should be styled depending on whether the user has voted on the element. However, since current_user
is not defined for a logged out user, accessing the page brings up an error. I don't want this to happen. How can I fix this so that if current_user
is nil, the user can still view the page, and the button receives the class 'unvoted'
?
:class => "up_arrow #{
current_user && current_user.votes_for(video).value == 1 ? 'voted' : 'unvoted'
}"
class: "up_arrow #{current_user.try(:votes_for, video).try(:value) == 1 ? 'voted' : 'unvoted'}"
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.