繁体   English   中英

respond_with - 如何回复文本

[英]respond_with - How to respond with a text

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

我想要这样的东西(我知道这不起作用)

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

任何想法??

谢谢!

看来这可能就是你要找的东西:

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

安德烈斯,

解决方案是这样的:

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

并在您的routes.rb:

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

暂无
暂无

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

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