代码之家  ›  专栏  ›  技术社区  ›  dennismonsewicz

Rails 3全局渲染

  •  1
  • dennismonsewicz  · 技术社区  · 14 年前

    有没有办法告诉整个控制器呈现特定的部分或文本?

    例子:

    class PageNotesController < ApplicationController
      render :text => "Testing"
      def index
        @notes = PageNotes.all
      end
    
      def show
        @note = PageNotes.find(params[:id])
      end
    
      def create
        @note = PageNotes.create(params[:note])
      end
    
    end
    

    现在很明显我可以进入每一个单独的方法,告诉它渲染一些东西,但我只是好奇是否这是可能的。

    提前谢谢!

    2 回复  |  直到 14 年前
        1
  •  0
  •   Sam å±±    14 年前

    可以告诉整个控制器渲染布局。

    layout 'some_layout'
    

    如果希望控制器呈现相同的操作,则可以创建一个操作并传递不同的选项,然后按参数条件查找结果。

    明确回答你的问题。我不知道。

        2
  •  2
  •   Jesse Wolgamott    14 年前

    你可以这么做。我不知道你为什么会想,但这里有办法。

    class PageNotesController < ApplicationController
      before_filter :write_out_testing
    
      ...
    
      protected
      def write_out_testing
        render :text=>"Testing
        false #do not execute the action originally requested.
      end
    
    end