Rails 3 global render

天大地大妈咪最大 提交于 2019-12-12 23:48:03

问题


Is there a way to tell an entire controller to render a particular partial or text?

Example:

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

Now obviously I can go into each individual method and tell it to render something, but I was just curious to know if this is possible.

Thanks in advance!


回答1:


you can tell an entire controller to render a layout.

layout 'some_layout'

if you want a controller to render the same action then you can create one action and pass it different options and find results by params conditions.

to explicitly answer your question. I don't know.




回答2:


You could do this. I'm unsure why you'd want to, but here's how.

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


来源:https://stackoverflow.com/questions/3981294/rails-3-global-render

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!