Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I explicitly declare a view from a Rails controller?

Tags:

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

like image 448
JP Richardson Avatar asked Nov 07 '08 15:11

JP Richardson


People also ask

What does controller do in Rails?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.

How can you tell Rails to render without a layout *?

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.

How do you render partials?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .


2 Answers

You could do something like the following using render:

respond_to do |format|     format.html { render :template => "weblog/show" } end 
like image 159
Kevin Kaske Avatar answered Oct 08 '22 01:10

Kevin Kaske


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} 
like image 45
Gabe Hollombe Avatar answered Oct 08 '22 00:10

Gabe Hollombe