限制Rails的方法呈现格式为html [英] Methods for limiting the Rails render format to html

查看:169
本文介绍了限制Rails的方法呈现格式为html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有html模板的Rails 2.1.2网站,例如jobs.html.erb,所以当我请求一个安宁的资源时:


www.mysite.com/jobs/1




$ b

然而,如果我请求,它会在html中呈现我的工作:



我得到这个错误:


模板丢失Missing模板
jobs / show.xml.erb在视图路径
c:/ workspace / mysite / app / views


更糟的是,我还可以请求类似于

lockquote

www.mysite.com/jobs/1.xyz


事实上,我看到错误:


模板丢失Missing模板
jobs / show.xyz.erb在视图路径
c:/ workspace / mysite / app / views


为了严格呈现HTML内容,告诉Rails我最不清楚的最简单的方法是什么



请注意:

$ ul

  • 我的一些控制器动作包含对render()方法的条件性调用,而另一些则使用默认的Rails行为,即如果不调用render(),则将呈现名为youraction.html.erb的模板。 >
  • 我的代码不使用responds_to()方法


    如果解决方案不是在render / responds_to级别,因为我将不得不修改大量的操作。也许有一种方法来配置Rails,只有html模板被渲染?

    解决方案
  • 如果你不想使用responds_to ,你可以这样做:

      class ApplicationController< ActionController :: Base 
    before_filter:allow_only_html_requests

    ...

    def allow_only_html_requests
    if params [:format]&& params [:format]!=html
    render:file => #{RAILS_ROOT} /public/404.html
    end
    end

    ...

    end

    这将在所有请求之前运行,并且只允许那些根本不指定格式或者指定html格式的通过。所有其他人得到404'd。你可以创建一个public / 406.html,如果你想返回406是不可接受的。


    I have a Rails 2.1.2 site that only has html templates e.g. jobs.html.erb, so when I request a restful resource:

    www.mysite.com/jobs/1

    It renders my job in html, however, if I request:

    www.mysite.com/jobs/1.xml

    I get the error:

    Template is missing Missing template jobs/show.xml.erb in view path c:/workspace/mysite/app/views

    What's worse is that I can also request something like

    www.mysite.com/jobs/1.xyz

    And indeed I see the error:

    Template is missing Missing template jobs/show.xyz.erb in view path c:/workspace/mysite/app/views

    To stricly present just html content, what is the cleanest and simplest way to tell Rails that I don't want to render anything other than .html.erb files.

    It is important to note that:

    • Some of my controller actions contain conditional calls to the render() method whilst others use the default Rails behaviour i.e. if you don't call render() then the template named youraction.html.erb will be rendered.
    • My code does not use the responds_to() method

    It would be great if the solution was not at the render/responds_to level as I would have to modify a significant number of actions. Perhaps there is a way to configure Rails so that only html templates are rendered?

    解决方案

    If you don't want to use responds_to, you can do this:

    class ApplicationController < ActionController::Base
      before_filter :allow_only_html_requests
    
      ...
    
      def allow_only_html_requests
        if params[:format] && params[:format] != "html"
          render :file => "#{RAILS_ROOT}/public/404.html"
        end
      end
    
      ...
    
    end
    

    That will run before all requests and only let those that do not specify format at all, or that specify html format through. All others get 404'd. You can create a public/406.html if you want to return 406 not acceptable.

    这篇关于限制Rails的方法呈现格式为html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆