简体   繁体   English

respond_with - 如何回复文本

[英]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... 我在rails应用程序中使用了respond_to和respond_with,但是在一个用例中我只需要响应一种资源格式的文本(:json)......但我找不到如何做到这一点...

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: 并在您的routes.rb:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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