Reply_with - How to reply with text

I use response_to and response_with in a rails application, but in one case I have to respond with text only for one of the resource formats (: json) ... But I can not find a way for this ...

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

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

Any idea ??

Thanks!

+4
source share
2 answers

It seems this may be what you are looking for:

 def create respond_to do |format| format.html format.json { render :text => "Successfully Done!" } end end 
+7
source

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 on your .rb routes:

 match '/text' => 'text#index', defaults: { format: 'text' } 
+6
source

Source: https://habr.com/ru/post/1438708/


All Articles