简体   繁体   中英

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!

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

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' }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM