我在rails应用程序中使用respond_to和respond_with,但在一个用例中,我只需要响应其中一种资源格式(:json)的文本......但我找不到如何做到这一点...
我想要这样的东西(我知道这行不通)
def create
...
respond_with(:json, render :text => "Successfully Done!")
end
任何想法??
谢谢!
我在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' }