禁用所有XML视图

时间:2011-04-28 05:16:45

标签: ruby-on-rails ruby-on-rails-3

在生产网站中,我有一个使用路线start#index定义的主页。

这可以按预期工作。

现在,一些抓取工具正在扫描页面中的/crossdomain.xml之类的内容,这会触发start控制器运行,并尝试返回XML视图。

不幸的是我没有定义XML视图或模板或其他任何内容,因此会生成错误消息(通过电子邮件,因此非常烦人):

[Exception] start#index (ActionView::MissingTemplate) "Missing template
start/index with {:handlers=>[:rjs, :rhtml, :builder, :rxml, :erb],
:formats=>[:xml], :locale=>[:crossdomain, :en]} in view paths

所以我猜有两种选择:

  • 在此应用中禁用所有XML / JSON并呈现默认的404页面。
  • 创建一个虚拟XML视图。

我更喜欢第一种选择,但我不确定如何做到这一点?感谢您的建议或最佳实践的链接。

编辑,根据要求,输出rake routes。我猜到第二行最后一行会出现一些错误?

            new_editor_session GET    /editors/sign_in(.:format)                                {:action=>"new", :controller=>"devise/sessions"}
                 editor_session POST   /editors/sign_in(.:format)                                {:action=>"create", :controller=>"devise/sessions"}
         destroy_editor_session GET    /editors/sign_out(.:format)                               {:action=>"destroy", :controller=>"devise/sessions"}
                                       /auth/:provider/callback(.:format)                        {:action=>"create", :controller=>"sessions"}
                        signout        /signout(.:format)                                        {:action=>"destroy", :controller=>"sessions"}
      photo_of_week_submissions GET    (/:locale)/submissions/photo_of_week(.:format)            {:action=>"photo_of_week", :controller=>"submissions"}
select_photo_of_week_submission GET    (/:locale)/submissions/:id/select_photo_of_week(.:format) {:action=>"select_photo_of_week", :controller=>"submissions"}
              accept_submission GET    (/:locale)/submissions/:id/accept(.:format)               {:action=>"accept", :controller=>"submissions"}
                    submissions GET    (/:locale)/submissions(.:format)                          {:action=>"index", :controller=>"submissions"}
                                POST   (/:locale)/submissions(.:format)                          {:action=>"create", :controller=>"submissions"}
                 new_submission GET    (/:locale)/submissions/new(.:format)                      {:action=>"new", :controller=>"submissions"}
                edit_submission GET    (/:locale)/submissions/:id/edit(.:format)                 {:action=>"edit", :controller=>"submissions"}
                     submission GET    (/:locale)/submissions/:id(.:format)                      {:action=>"show", :controller=>"submissions"}
                                PUT    (/:locale)/submissions/:id(.:format)                      {:action=>"update", :controller=>"submissions"}
                                DELETE (/:locale)/submissions/:id(.:format)                      {:action=>"destroy", :controller=>"submissions"}
                          login        (/:locale)/login(.:format)                                {:to=>#<Proc:0x0000000103871938@/Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/routing/mapper.rb:366>}
                         design        (/:locale)/design(.:format)                               {:action=>"design", :controller=>"page"}
                        gallery        (/:locale)/gallery(.:format)                              {:action=>"gallery", :controller=>"page"}
                       features        (/:locale)/features(.:format)                             {:action=>"features", :controller=>"page"}
                    competition        (/:locale)/competition(.:format)                          {:action=>"index", :controller=>"competition"}
                facebook_albums        (/:locale)/facebook-albums(.:format)                      {:action=>"facebook_albums", :controller=>"competition"}
                facebook_photos        (/:locale)/facebook-photos(.:format)                      {:action=>"facebook_photos", :controller=>"competition"}
                facebook_upload        (/:locale)/facebook-upload(.:format)                      {:action=>"facebook_upload", :controller=>"competition"}
                           root        (/:locale)(.:format)                                      {:action=>"index", :controller=>"start"}
                           root        /(.:format)                                               {:action=>"index", :controller=>"start"}

2 个答案:

答案 0 :(得分:1)

您可以使用路线约束,以便只接受html作为格式。这将为您提供首选。

查看thisthis

答案 1 :(得分:1)

控制器操作的响应块包含哪些内容?如果您已离开默认区块:

respond_to do |format|
  format.html { redirect_to(foobar_url) }
  format.xml  { head :ok }
end

但尚未定义XML模板,您将收到错误消息。删除format.xml(或者如果你只想要HTML,你可以完全省去respond_to块),除了HTML之外的任何格式请求都将失败。