No “(.:format)” in Rails 3.1 when trying to route site root

房东的猫 提交于 2019-12-12 01:55:32

问题


Since upgrading to Rails 3.1, I've been having a problem with my site's routing. Previously, with Rails 3.0, I was able to do this in my router config:

resources :quotes, :path => ""
root :to => "quotes#index"

That would give me routes like GET /(.:format) and such, which is what I want since the index action of my QuotesController can also return data in JSON, XML and ATOM.

Now, since upgrading to Rails 3.1, the routes have been showing up like this: GET /. The (.:format) is gone, and trying to access the /.atom URL doesn't work anymore. How can I get this functionality back?

EDIT: "rake routes CONTROLLER=quotes" outputs the following:

   about_quotes GET    /about(.:format)    {:action=>"about", :controller=>"quotes"}
     top_quotes GET    /top(.:format)      {:action=>"top", :controller=>"quotes"}
  random_quotes GET    /random(.:format)   {:action=>"random", :controller=>"quotes"}
  search_quotes GET    /search(.:format)   {:action=>"searchform", :controller=>"quotes"}
                POST   /search(.:format)   {:action=>"search", :controller=>"quotes"}
nonsense_quotes GET    /nonsense(.:format) {:action=>"nonsense", :controller=>"quotes"}
    tags_quotes GET    /tags(.:format)     {:action=>"tags", :controller=>"quotes"}
     tag_quotes GET    /tags/:id(.:format) {:action=>"tag", :controller=>"quotes"}
   stats_quotes GET    /stats(.:format)    {:action=>"stats", :controller=>"quotes"}
       up_quote GET    /:id/up(.:format)   {:action=>"up", :controller=>"quotes"}
     down_quote GET    /:id/down(.:format) {:action=>"down", :controller=>"quotes"}
         quotes GET    /                   {:action=>"index", :controller=>"quotes"}
                POST   /                   {:action=>"create", :controller=>"quotes"}
      new_quote GET    /new(.:format)      {:action=>"new", :controller=>"quotes"}
     edit_quote GET    /:id/edit(.:format) {:action=>"edit", :controller=>"quotes"}
          quote GET    /:id(.:format)      {:action=>"show", :controller=>"quotes"}
                PUT    /:id(.:format)      {:action=>"update", :controller=>"quotes"}
                DELETE /:id(.:format)      {:action=>"destroy", :controller=>"quotes"}
           root        /                   {:controller=>"quotes", :action=>"index"}

回答1:


Okay, so, I've decided that this was probably something that the Rails team did intentionally to discourage URLs like "/.atom" (because really, does that look like something that should happen?), so I'm now using a second route (get "latest", :action => :index) for the format URLs (so, like, "/latest.atom") instead.



来源:https://stackoverflow.com/questions/7686587/no-format-in-rails-3-1-when-trying-to-route-site-root

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