Moosa
Moosa

Reputation: 3216

Rails 4 - Exclude page from part of application.html

In my application.html file I have a block of code that displays error messages up at the top of the page. I want to exclude this block of code from a certain page. How would I reference the specific page?

For example: if path != thankyou_path #codeblock end? What would the syntax be for rails to exclude a block of code from running on a certain page?

Upvotes: 0

Views: 92

Answers (2)

Vince V.
Vince V.

Reputation: 3143

While the current_page? code is good for one page, this is not very scaleable.

Another way of doing this is by adding a method in the application controller like this:

def disable_error_messages
  @disable_error_messages = true
end

then in your view you can just test like this:

unless @disable_error_messages
  <div>Error messages</div>
end

that way you can use this piece of code on different pages without the need to pin it on something specific. Another advantage is when you refactor names, this still works.

Upvotes: 2

Vucko
Vucko

Reputation: 20834

I believe you can use current_page?, like:

unless current_page?(controller: 'controller_name', action: 'action_name')
    <div>Error messages</div>
end

Or with path:

unless current_page?(some_path)
    <div>Error messages</div>
end

Upvotes: 2

Related Questions