我试图理解link_to_if,
致电:
<%= link_to_if(!current_user.nil?, "My profile", profile_path(current_user.profile)) %> <br>
<%= link_to_if(!current_user.nil?, "Edit profile", edit_profile_path(current_user.id) {}) %> <br>
它抛出了这个错误。
nil的未定义方法`profile':NilClass
当然因为我已经注销但是不应该调用link_to_if停止profile_path(current_user.profile)
或者我没有错误地实现它?
答案 0 :(得分:4)
但不应该
link_to_if
停止
link_to_if
甚至没有&#34;开始&#34;然而。在调用方法之前,将评估其所有参数。在这种情况下,条件和profile_path
(失败)。您应该将链接包装在外部条件中。
<% if current_user %>
<%= link_to "My profile", profile_path(current_user.profile) %>
<% end %>
答案 1 :(得分:1)
尝试以下
如果你需要没有外部条件,那么看起来像这样
<%= link_to_unless(current_user.nil?, "My profile", profile_path(current_user.profile)) %>
或者
<% if current_user %>
<%= link_to "My profile", profile_path(current_user.profile) %>
<% end %>
希望有所帮助