Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 respond_with custom template

I have a latest action in my controller. This action just grabs the last record and renders the show template.

class PicturesController < ApplicationController
  respond_to :html, :json, :xml

  def latest
    @picture = Picture.last

    respond_with @picture, template: 'pictures/show'
  end
end

Is there a cleaner way to supply template? Seems redundant to have to supply the pictures/ portion for the HTML format since this is the Sites controller.

like image 670
mikeycgto Avatar asked Nov 12 '12 14:11

mikeycgto


People also ask

How do I change the render behavior in rails?

There are a variety of ways to customize the behavior of render. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.

What does it mean to create a response in rails?

In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views.

How do I create an HTTP response from a Rails controller?

From the controller's point of view, there are three ways to create an HTTP response: You've heard that Rails promotes "convention over configuration". Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to valid routes.

How to render content along with non-content status codes in rails?

If you try to render content along with a non-content status code (100-199, 204, 205, or 304), it will be dropped from the response. Rails uses the format specified in the request (or :html by default). You can change this passing the :formats option with a symbol or an array:


2 Answers

I've done this similarly to @Dario Barrionuevo, but I needed to preserve XML & JSON formats and wasn't happy with doing a respond_to block, since I'm trying to use the respond_with responders. Turns out you can do this.

class PicturesController < ApplicationController
  respond_to :html, :json, :xml

  def latest
    @picture = Picture.last

    respond_with(@picture) do |format|
      format.html { render :show }
    end
  end
end

The default behavior will run as desired for JSON & XML. You only have to specify the one behavior you need to override (the HTML response) instead of all three.

Source is here.

like image 43
Ben Kreeger Avatar answered Sep 24 '22 21:09

Ben Kreeger


If the template you want to render, belongs to the same controller, you can write it just like this:

class PicturesController < ApplicationController
  def latest
    @picture = Picture.last

    render :show
  end
end

It is not necessary the pictures/ path. You can go deeper here: Layouts and Rendering in Rails

If you need to preserve xml and json formats, you can do:

class PicturesController < ApplicationController
  def latest
    @picture = Picture.last

    respond_to do |format|
      format.html {render :show}
      format.json {render json: @picture}
      format.xml {render xml: @picture}
    end

  end
end
like image 125
Dario Barrionuevo Avatar answered Sep 26 '22 21:09

Dario Barrionuevo