Could you tell me how to disable the .:format options in rails routes? I only need html...
Asked
Active
Viewed 1.2k times
4 Answers
51
In 3.1.1 at least you can add , :format => false
to the end of the route.
Found here: http://guides.rubyonrails.org/routing.html#request-based-constraints under section 3.11 Route Globbing
eg..
match '*pages' => 'pages#show', :format => false
Which would allow params[:pages] to include a period.

Sam Saffron
- 128,308
- 78
- 326
- 506

Jim Morris
- 2,870
- 24
- 15
16
http://guides.rubyonrails.org/routing.html#request-based-constraints
This will constrain your routes to accept only html format:
constraints :format => "html" do
resources :posts do
resources :comments
end
end
However, it won't remove (.:format)
part from your rake routes
output.

Heikki
- 15,329
- 2
- 54
- 49
-
1I know I'm late, but with rails 3.2.13 this causes requests coming from curl to render a 404 error, and my website was totally wiped out from google ... – Intrepidd Apr 17 '13 at 13:40
14
You can wrap you routes around a scope (Rails 4):
scope format: false do
# your routes here
end

Duncan Robertson
- 596
- 5
- 10
-
1exactly what I was searching, thanks a lot! Btw, working in Rails 5, too. – Florian Koch Mar 03 '17 at 15:47
-
4
If you want pretty URLs and you don't like :format => false
you might try this:
# :format must match the empty string
constraints :format => // do
resources :monkeys
end
Even using with_options
, the :format => false
option is cumbersome, especially if you have a lot of routes.

Jared Beck
- 16,796
- 9
- 72
- 97