在每个页面上,我们都为 guest 用户设置了这样的条件。
<% if not_guest? %>
<% link_to "show", path %>
<% end %>
<% if not_guest? %>
<% link_to "delete", path %>
<% end %>
<% if not_guest? %>
<% link_to "edit", path %>
<% end %>
访客用户应该显示或不显示哪个链接。
有没有更好的方法来处理这种情况,而不是为每个链接编写条件?
最佳答案
做一个助手:
#helpers/application_helper.rb
def link_to_unless_guest(*args)
if not_guest
link_to(*args)
end
end
然后调用like
<% link_to_unless_guest "show", path %>
关于ruby-on-rails - 如何处理 guest 用户的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6122048/