Hemanth
Hemanth

Reputation: 5145

How to change the content of a view page based on current url

My app has a navigation bar with options:-
"create post", "Show posts owned by me", "Show All posts"
But of I navigate to the page "show posts owned by me", the navigation bar should no longer display the option "show posts owned by me". Is the any api such as current_url_path so that I can compare current_url_path api's output with "desired_url_check_path". and modify the view output accordingly.

Upvotes: 0

Views: 265

Answers (2)

Laz
Laz

Reputation: 3538

There is the link_to_unless_current function:

<%=
   link_to_unless_current("Show posts owned by me", { :controller => "posts", :action => "index" }) do
      # Whatever code you want to occur
   end
%>

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_unless_current

You should also look at the link_to_unless function.

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_unless

Upvotes: 1

Trip
Trip

Reputation: 27124

Hey Saran. Yes, this is a helper I put in my application_helper.rb :

def main_nav_select(name, url)
  url = params[:q].blank? ? url : "#{url}?q=#{params[:q]}"
  if current_page? url
    content_tag(:li, "#{link_to(name, url)}", :class => 'selected')
  else
    content_tag(:li, "#{link_to(name, url)}")
  end
end

The real catch here is curent_page? url

I implement it like so :

= main_nav_select 'Main Page', admin_banners_path

Upvotes: 0

Related Questions