Merkelst
Merkelst

Reputation: 311

Hide button if you are on some page

I need to show two buttons created with button_to on some pages (Example: list/id1/edit) and hide one button on all other pages (Example: list/new). I have this:

= button_link t("list.save"), list_path(@list.save_id), class: "button"    
= button_link t("list.cancel"), list_path(@list.hashed_id), class: "button"

How can I create "if you are on page X show one button, else - 2 buttons"?

Upvotes: 1

Views: 838

Answers (2)

SRack
SRack

Reputation: 12203

I like @arjun's answer (particularly with @RyanWilcox's comment), though just to throw something else into the ring, you'll also have access to the controller_name and action_name helpers...

i.e.

unless controller_name == 'list' && action_name == 'new'
  show_the_button
end

or

- unless controller_name == 'list' && action_name == 'new'
  = button_link t("list.save"), list_path(@list.save_id), class: "button"    
  = button_link t("list.cancel"), list_path(@list.hashed_id), class: "button"

That can be pretty flexible in terms of restricting / allowing certain controllers and / or actions.

Upvotes: 2

arjun
arjun

Reputation: 1614

Please check this doc - http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page-3F

Use it as

current_page?('http://www.example.com/shop/checkout')

Upvotes: 4

Related Questions