如何解决如何处理访客用户的链接?
| 在每个页面上,访客用户都有这样的条件。<% 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
然后像
<% link_to_unless_guest \"show\",path %>
, def link_to_editable(*args)
options = args.extract_options![:parent]
html_tag = options.nil? ? nil : options.delete(:html_tag)
if not_guest
unless html_tag.nil?
content_tag html_tag,options do
link_to(*args)
end
else
link_to(*args)
end
end
end
<%= link_to_editable \'Show\',path,:parent => {:html_tag => \"li\",:style => \"border-top:1px solid #A2A2A2;\",:class => \"left\"} %>
<%= link_to_editable \'Show\',path %>
@Max根据我的需要提供了修改的帮助程序。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。