Ruby on Rails: fixing this route to show a 404
I am working on creating a new controller/route to only allow an HTTP GET and HTTP POST. The URL should be to /foo. Naturally my route looks like the following:
map.connect 'foo',
:conditions => { :method => :get },
:controller => "foo",
:action => "display_something"
map.connect 'foo',
:conditions => { :method => :post },
:controller => "foo",
:action => "register_foo"
The part of this that I do not understand is that going to http://example/foo/1 brings up 'Unknown action'. I don't want /foo/N to be accessible. It should give the user a 404 instead of throwing ActionController::UnknownAction.
Looking closer at the RouteSet object we have the following:
>> rs.recognize_path "/foo/1"
=> {:controller=>"foo", :action=>"1"}
So it finds a route, and that's why we do not have a 404.
Do I have to hack around with method_missing in ApplicationController to throw a 404 whenever a开发者_开发知识库n action method is missing? I just want a 404 to get thrown if there is no action. In my application /foo/N does not make sense and I do not want to confuse the user.
Looking @ the API docs for ActionController::Rescue
, it appears that the rescue response for ActionController::UnknownAction
defaults to => :not_found
. I believe, in production mode, that should render a 404. Try running your app in production mode & see how it responds (or, you should just be able to see the HTTP return code in the log.
精彩评论