SupremeA
SupremeA

Reputation: 1621

What syntax do I use if I want to confirm a user is on a certain page?

I have a rails app and in the the application layout view I want the header to show a link ONLY if a user is on a certain page.

How would I write that?

Currently I did

<% if welcome_index2_path? %>
    blah...blah...blah
<% else %>
    blah...blah...blah
<% end %>

But my if statement is not right, I need the correct syntax.

Upvotes: 0

Views: 31

Answers (4)

Padhu
Padhu

Reputation: 1580

You should do something like this.

<% if current_page?(:controller => 'yours', :action => 'yours') %>

Upvotes: 0

Max Williams
Max Williams

Reputation: 32955

Try this

<% if request.request_uri == welcome_index2_path %>
    ....

it would be more useful for you to use an actual specific example from your code, as it might be that you want to ignore ids in the url path and just want to test the current controller and action, but i can't tell from your example.

Upvotes: 0

user4187589
user4187589

Reputation:

Just use the current_page? helper!

<% if current_page? welcome_index2_path %>

As Yuri said, you can also use yield.

Upvotes: 2

Yury Lebedev
Yury Lebedev

Reputation: 4015

You can use the yield with content_for:

In your layout:

<%= yield :links %>

In the view for some action, where you need the link:

<% content_for :links do %>
  <%= link_to 'Foo', bar_path %>
<% end %>

You also have a content_for? helper method, which returns true, if the content for the given key is given:

content_for?(:foo) => false
content_for(:foo, :bar)
content_for?(:foo) => true

Upvotes: 0

Related Questions