[英]Want to show my index view to non-logged in users
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. 因此,我不会对索引操作应用身份验证筛选器,但是由于以下这一行,未登录的用户仍然无法访问索引视图
:class => "up_arrow #{current_user.votes_for(video).value == 1 ? 'voted' : 'unvoted' }"
,它根据用户是否对元素进行投票来确定是否应设置按钮的样式。 However, since current_user
is not defined for a logged out user, accessing the page brings up an error. 但是,由于未为注销用户定义
current_user
,因此访问该页面将引发错误。 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'
? 我如何解决此问题,以便如果
current_user
为nil,用户仍然可以查看页面,并且按钮收到'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'}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.