rails 3 custom mime type - default view format

前端 未结 2 1684
一生所求
一生所求 2021-01-03 15:20

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).<

相关标签:
2条回答
  • 2021-01-03 15:40

    You can use the layout method in your controller directly:

    class ProductsController < ApplicationController
      layout "product", :except => [:index, :rss]
    end
    

    Or to use no layout at all:

    class ProductsController < ApplicationController
      layout nil
    end
    

    Check out the guide for more info.

    0 讨论(0)
  • 2021-01-03 15:48

    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

    0 讨论(0)
提交回复
热议问题