kru*_*hah 1 ruby ruby-on-rails
在每个页面上,我们都为访客用户提供了这样的条件.
<% if not_guest? %>
<% link_to "show", path %>
<% end %>
<% if not_guest? %>
<% link_to "delete", path %>
<% end %>
<% if not_guest? %>
<% link_to "edit", path %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
对于访客用户应该显示或不显示哪个链接.
有没有更好的方法来处理这种情况,而不是为每个链接编写条件?
做个帮手:
#helpers/application_helper.rb
def link_to_unless_guest(*args)
if not_guest
link_to(*args)
end
end
Run Code Online (Sandbox Code Playgroud)
然后打电话给
<% link_to_unless_guest "show", path %>
Run Code Online (Sandbox Code Playgroud)