如果您不想渲染任何内容,您可以传递 html、文本或只传递响应标头。
以 Html 为例,只需渲染您想要的视图:
def tracks
binding.pry
# some condition determining val accordingly
render 'tracks' #tracks.html.erb
end
$.post("/xxx/tracks", { name: "xx", time: "xx", pa: "xx" }, function(data) {
$("selector").append(data)
}).error(function(response, data){
alert('error processing the request')
})
对于文本:
def tracks
binding.pry
# some condition determining val accordingly
if expression
render text: 'success', status: :ok
else
render text: 'Error processing your request', status :bad_request
end
end
$.post("/xxx/tracks", { name: "xx", time: "xx", pa: "xx" }, function(response, data) {
alert(data.responseText)
}).error(function(response, data){
alert(data.responseText)
})
仅渲染标题
def tracks
binding.pry
# some condition determining val accordingly
if expression
head :created
else
head :unprocessable_entity
end
end
$.post("/xxx/tracks", { name: "xx", time: "xx", pa: "xx" }, function() {
alert('seccess')
}).error(function(){
alert('error')
})
您也可以渲染 json 数据。