I've got a requirement to specify a named route in a Ruby on Rails project that returns the public/404.html page along with the 404 server response code.
Leaving it blank is not an option, please don't question why, it just is :) It absolutely must be a named route, or a map.connect entry would do.
Something like this would be great:
map.my_named_route '/some/route/', :response => '404'
Anyone have any idea what's the easiest way to do something like this. I could create a controller method which renders the 404.html file but thought there might be an existing cleaner way to do this. Looking forward to any responses - thanks,
Eliot
a) add that last route to your route list. b) create a controller (in my example, i called it StaticContentController) with an Action method (in my example, i added a method called PageNotFound(..)) add logic this method to display the 404 page not found, View. If you still have the default route (I.E.
Every Website needs a 404 page if the URL does not exist or the URL might have been changed. To set up a 404 page in the angular routing, we have to first create a component to display whenever a 404 error occurred. In the following approach, we will create a simple react component called PagenotfoundComponent.
You can route to a rack endpoint (rails 3) that vends a simple 404:
match 'my/route', to: proc { [404, {}, ['']] }
This is particularly handy, for example, to define a named route to your omniauth endpoint:
match 'auth/:action', to: proc { [404, {}, ['']] }, as: :omniauth_authorize
In your routes.rb:
map.my_404 '/ohnoes', :controller => 'foobar', :action => 'ohnoes'
In FoobarController
:
def ohnoes render :text => "Not found", :status => 404 end
If you need to render the same 404 file as a normal 404, you can do that with render :file
.
See ActionController::Base documentation for examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With