views:

384

answers:

4

I want to explicitly call a view from my controller.

Right now I have:

def some_action
  .. do something ...
  respond_to do |format|
    format.xml
  end
end

... then it calls my some_action.xml.builder view. How can I call some other view? Is there a parameter in respond_to I'm missing?

Thanks,

JP

+6  A: 

See the Rendering section of the ActionController::Base documentation for the different ways you can control what to render.

You can tell Rails to render a specific view (template) like this:

# Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
  render :template => "weblog/show"

# Renders the template with a local variable
  render :template => "weblog/show", :locals => {:customer => Customer.new}
Gabe Hollombe
+6  A: 

You could do something like the following using render:

respond_to do |format|
    format.html { render :template => "weblog/show" }
end
Kevin Kaske
+2  A: 

You can also pass :action, or :controller if that's more convenient.

respond_to do |format|
    format.html { render :action => 'show' }
end
Cameron Price