开发者

How to handle links for guest user?

On every page we have a condition like this for guest user.

<% if not_guest? %> 
<% link_to "show", path %>
<% end %>

<% if not_guest? %> 
<% link_to "delete", path %>
<% end %>

<% if not_guest? %> 
<% link_to "edit", path %>
<% end %>

for which link should appear or not for guest user.

Are t开发者_如何学运维here any better ways to handle this scenario instead of writing the conditions for every link ?


Make a helper:

#helpers/application_helper.rb
def link_to_unless_guest(*args)
  if not_guest
    link_to(*args)
  end
end

Then call like

<% 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 %>

Modified helper which is provided by @Max as per my need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜