Pigueiras
Pigueiras

Reputation: 19356

How to render the same view for all actions in the same controller?

I want to render the same view in the controller for all my actions. For instance, I have:

class FooController 
  def action1
    #action1 code
  end

  # [...]

  def actionN
    #actionN code
  end 

end

And then I want that all actions render generic_page.html.

I tried with:

...
after_filter :render_generic

...
private 
  def render_generic
    render 'generic_page'
  end

But render method is being called before after_filter and I'm getting a Template Missing error because it isn't rendering the correct template. Does anyone know a simple solution for that problem?

Upvotes: 2

Views: 1997

Answers (2)

Zoran Majstorovic
Zoran Majstorovic

Reputation: 1609

using rescue_from

    class FooController
      rescue_from ActionView::MissingTemplate do
        render 'generic_page'
      end

    end

Upvotes: 6

Unixmonkey
Unixmonkey

Reputation: 18784

I would just be explicit:

def action1
  #action1 code
  render_generic
end

def actionN
  #actionN code
  render_generic
end

private
  def render_generic
    render 'generic_page'
  end

Upvotes: 2

Related Questions