3

Is there a way in Rails to add a class to a html tag if the current page rendered a 404 error.

On a controller of mine, if a user visits the wrong website it renders a file in the public error with a 404 error like this

else
  render file: 'public/error', status: 404, formats: [:html]
end

I want to add a class to my footer, hidden, to hide the footer if this page is rendered. I've tried this with no luck (but it was more of a guess)

<%= "hidden" if current_page?(status: 404) %>

Any suggestions??

Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75
Justin
  • 4,922
  • 2
  • 27
  • 69
  • You have `400` there, not `404`. If you change that, does your code work? – TheDude Jan 14 '14 at 00:28
  • Haha good eye, but still no luck. I figured out a way around it. I just made a stylesheet on that file and hid the footer from the stylesheet. :) Thanks for taking a look at it though! I am still curious about the answer though, so I'll leave it up in case someone knows the answer other than the side trick I did – Justin Jan 14 '14 at 00:31
  • In Rails 3, you can do `<%= "hidden" if controller.status == 404 %>` but I'm too lazy to test that in 4 so I'm writing a comment, not an answer :) – mechanicalfish Jan 14 '14 at 00:50
  • lol thanks i love it @mechanicalfish. That being said it didn't work :) Thanks anyway for taking a look at it – Justin Jan 14 '14 at 00:57

2 Answers2

1

You could make it more generic and set an @variable to tell the footer to hide. So if later on you want a 500 page without footer you can use the same trick.

controller

else
  @hide_footer = true
  render file: 'public/error', status: 404, formats: [:html]
end

view

<%= "hidden" if @hide_footer %>

And you don't need to change anything else.

Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75
0

You could just render a separate layout for your error files

else
    render :template => "shared/404", :layout => 'errorlayout', :status => 404
end

You might want to have a look at these questions and answers as well:

Dynamic error pages in Rails 3

how to remove header and footer from some of the pages in ruby on rails

How to hide footer layout on a particular page? - This is basically Ismael Abreu suggestion and is probably the easiest.

Community
  • 1
  • 1
ChrisBarthol
  • 4,871
  • 2
  • 21
  • 24