サブ URL に似たものを使用する方法を探していlink_to_unless_currentます。たとえば、これらの両方の URL の場合:
/entity_name/
/entity_name/123
リンクにはなりません。Railsでそれを達成するにはどうすればよいですか?
UPD:自分のカスタム方法で解決しました。Ruby に詳しい方、よろしければ教えてください。
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
使用法:
<%= link_to_unless_current_or_sub("Users", users_path) %>