Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 rails 3自定义mime类型-默认视图格式_Ruby On Rails 3_Views_Format_Alias - Fatal编程技术网

Ruby on rails 3 rails 3自定义mime类型-默认视图格式

Ruby on rails 3 rails 3自定义mime类型-默认视图格式,ruby-on-rails-3,views,format,alias,Ruby On Rails 3,Views,Format,Alias,我需要渲染一些没有布局的视图。 要跳过控制器操作中的行:render:layout=>false和if-else逻辑, 我有定制的mime类型,比如phtml(纯html) 这种格式需要呈现相同的html视图,但只需要没有布局。我用应用程序中的这段代码来完成这项工作。控制器: before_filter proc { |controller| if params[:format] && params[:format]=='phtml' controller.acti

我需要渲染一些没有布局的视图。 要跳过控制器操作中的行:render:layout=>false和if-else逻辑, 我有定制的mime类型,比如phtml(纯html)

这种格式需要呈现相同的html视图,但只需要没有布局。我用应用程序中的这段代码来完成这项工作。控制器:

 before_filter proc { |controller|
  if params[:format] && params[:format]=='phtml'
    controller.action_has_layout = false
    controller.request.format    = 'html'

  end
  }
controller.collect_mimes_from_class_level.include?(:plain_html)
首先,这很难看,其次,我无法再从控制器控制这种格式:

respond_to :phtml,:only=>:index
因为它将始终以请求的格式phtml呈现视图。 有更好的解决办法吗?例子?如何使用alias视图格式


非常感谢

您可以直接在控制器中使用
布局
方法:

class ProductsController < ApplicationController
  layout "product", :except => [:index, :rss]
end
class ProductsController[:索引,:rss]
结束
或者完全不使用布局:

class ProductsController < ApplicationController
  layout nil
end
class ProductsController

查看更多信息。

我没有找到更好的解决方案,只更新了我之前的示例:

 before_filter proc { |controller|
    if params[:format] && params[:format]=='plain_html' && controller.collect_mimes_from_class_level.include?(:plain_html)
      controller.action_has_layout = false
      controller.request.format    = 'html'
    end
  }
我添加这一行是为了检查控制器中定义的新格式:

 before_filter proc { |controller|
  if params[:format] && params[:format]=='phtml'
    controller.action_has_layout = false
    controller.request.format    = 'html'

  end
  }
controller.collect_mimes_from_class_level.include?(:plain_html)
现在我们可以使用全新的格式来呈现我们的标准html VEW,而不是为新格式构建新视图

如果我们不想共享现有的html代码,而是基于请求的格式构建不同的逻辑,那么这将非常有用

例如,我们可以轻松地准备html内容进行打印,如:

class PagesController < ActionController::Base

layout 'print',:only=>:show
respond_to :plain_html,:only=>[:show]

  def show
    Page.find(1)
    respond_with @page
  end
end
我希望有人会觉得这很有用

如果你有更好的方法,请与我们分享


关于我一直认为
。phtml
是用于PHP/PERL风格的HTML。你说得对。它可以是。纯html…这不是重点…我也有这个问题,我还需要一种方式来呈现“纯html”。必须有一个好的方法来做到这一点,因为肯定会出现很多。我不想重复我所有的控制器动作(不是很干)。我还不知道一个好的解决方案,但是你的信息帮助了我,谢谢。关于内容类型,我个人仍然将我的自定义扩展声明为“text/html”,因为这就是它——只是使用了一个不同的扩展。就像“.htm”和“.html”都是“text/html”一样。谢谢你的回答。这不是我的解决方案,因为我需要两种情况在同一个行动(有或没有布局)。这就是我需要另一种格式的原因。这对于以下情况非常有用:打印、从javascript获取“普通”html等。
layout
也接受一个proc,这将允许您根据请求的格式有条件地选择布局。查看指南()了解更多信息。好的……它可以解决布局问题。但它不能解决html格式的问题。它仍在寻找phtml或(纯html)模板。我需要一个解决这个问题的办法。最后,我们仍然有一个逻辑进入布局程序块。:)。我正在寻找一种解决方案,用类似于alias to_plain_html,to_html的东西覆盖AppResponder