views:

74

answers:

1

Hey,

I'm looking for a way to use something similar to link_to_unless_current but for sub URLs, so for instance for both these URLs:

/entity_name/
/entity_name/123

it will not result as a link. How can I achieve that in Rails?

UPD: Resolved it my own custom way, please Ruby savvy let me know if it's good.

module ApplicationHelper

  def link_to_unless_current_or_sub(name, options = {}, html_options = {}, &block)
    link_to_unless current_page_or_sub?(options), name, options, html_options, &block
  end

private

  def current_page_or_sub?(options)
    url_string = CGI.unescapeHTML(url_for(options))
    request = @controller.request
    if url_string.index("?")
      request_uri = request.request_uri
    else
      request_uri = request.request_uri.split('?').first
    end

    request_uri.starts_with?(url_string)
  end

end

Usage:

<%= link_to_unless_current_or_sub("Users", users_path) %>
A: 

i don't think such helper method exist

you have to write something like following

<% if params[:id] %>
       "Home"
<% else %>
       <%= link_to "Home", { :action => "index" }) %>
<% end %>
Salil
I don't want to do that as I can have inner controllers under entity_name: /blogs/1/comments.
Vitaly