Disable :.format routes in rails3

孤街醉人 提交于 2019-12-20 08:57:13

问题


Could you tell me how to disable the .:format options in rails routes? I only need html...


回答1:


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.




回答2:


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.




回答3:


You can wrap you routes around a scope (Rails 4):

scope format: false do
  # your routes here
end



回答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.



来源:https://stackoverflow.com/questions/4579652/disable-format-routes-in-rails3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!