Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

respond_with - How to respond with a text

I'm using the respond_to and respond_with in a rails app, but in one use case I need to respond with just a text for just one of the resource formats (:json)... But I can't find how to do this...

I want something like this (I know this doesn't work)

def create
    ...
    respond_with(:json, render :text => "Successfully Done!")
end

Any Idea??

Thanks!

like image 702
Andres Avatar asked Oct 09 '12 17:10

Andres


2 Answers

It seems that this may be what you are looking for:

def create
  respond_to do |format|
    format.html
    format.json { render :text => "Successfully Done!" }
  end
end
like image 121
Max Dunn Avatar answered Nov 11 '22 16:11

Max Dunn


Andres,

The solution is this:

class TextController < ApplicationController
  respond_to :json, :text

  def index
    respond_with do |format|
      format.json {
        render :text => "I'm a text provided by json format"
      }
      format.text {
        render :text => "I'm a text"
      }
    end
  end
end

And at your routes.rb:

match '/text' => 'text#index', defaults: { format: 'text' }
like image 40
Bruno Arueira Avatar answered Nov 11 '22 17:11

Bruno Arueira