导轨3自定义MIME类型 - 默认视图格式 [英] rails 3 custom mime type - default view format

查看:166
本文介绍了导轨3自定义MIME类型 - 默认视图格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要渲染一些没有布局的视图。
跳过一行:render:layout => false,如果其他控制器动作的逻辑
i有自定义MIME类型,如phtml(纯HTML)。

  Mime :: Type.registertext / phtml,:phtml 

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

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

end
}

首先,这是丑陋的,第二我不能从控制器的方式控制这种格式:

  respond_to:phtml,:only =>:index 

,因为它将始终以请求的格式phtml呈现视图。
是否有更好的解决方案?例?我怎样才能查看格式?



非常感谢

解决方案



  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
}


$ b我添加了这一行来检查是否定义了一个新的格式到我们的控制器中:

 controller.collect_mimes_from_class_level.include?(:plain_html)

现在我们可以有全新的格式,这将使我们的标准的html vews,而不是建立新的格式新视图。
$ b

如果我们不想共享现有的html代码,而是根据请求的格式构建不同的逻辑,这会很有用。

例如,我们可以很容易地准备我们的html内容来打印,如:

  class PagesController< ActionController :: Base 
$ b $ layout'print',:only => show
respond_to:plain_html,:only => [:show]

def显示
Page.find(1)
respond_with @page
end
end

请求将如下所示:

  http://www.example.com/pages/1 .plain_html 

我希望有人会觉得这个有用。



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



问候


I need to render some views without layout. To skip line :render :layout=>false and if else logic from controller actions, i have custom mime type like phtml (plain html).

Mime::Type.register "text/phtml", :phtml

this format need to render the same html views, but only without layout. I complete this with this chunk of code in app. controller:

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

  end
  }

First, this is ugly, second i can't any more control this format from controller in the way:

respond_to :phtml,:only=>:index

because it will always render view with requested format phtml. is there a better solution? example? how can i alias view formats?

Thank a lot

解决方案

I haven't found better solution,only update to my previous example:

 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
  }

i added this line to check is a new format defined into our controller:

controller.collect_mimes_from_class_level.include?(:plain_html)

Now we can have full new format which will render our standard html vews rather the build new views for the new format.

This can be useful if we wont to share existing html code, but build different logic based on requested format.

For example, we can easily prepare our html content for print like:

class PagesController < ActionController::Base

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

  def show
    Page.find(1)
    respond_with @page
  end
end

And request will be like:

http://www.example.com/pages/1.plain_html

I hope that someone will find this useful.

If u have a better approach for doing this, please share with us.

Regards

这篇关于导轨3自定义MIME类型 - 默认视图格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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