0
我有许多视图都包含检查当前用户是否与正在查看的用户不同的视图等。例如,在用户显示页面上有一个按钮允许用户关注另一个用户,如果用户正在查看他们自己的配置文件,则不会显示该用户。允许未经身份验证的用户访问利用current_user的视图
<% if current_user != @user %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'users/unfollow' %>
<% else %>
<%= render 'users/follow' %>
<% end %>
</div>
<% end %>
问题在于,如果您未登录,rails会引发错误。
undefined method `id' for nil:NilClass
<% if current_user.id == @user.id %>
<%= link_to "Edit Profile", edit_user_registration_path(@user) %>
<% else %>
<%= link_to 'message', new_message_path(receiver_id: @user.id) %>
...
我不想强制人们登录或注册查看索引或显示页面。我怎样才能解决这个问题?
使用user_signed_in?检查是否有用户登录。 –
谢谢,我想这一定是我去睡觉的时候了。 :)把它作为答案,我会标记它解决。 – ddonche