是否可以使用html而不是json响应json请求?
我想这样做:
respond_to do |format|
format.html { render 'page' }
format.json { render 'page' } #render an html page instead of a JSON response
end
但是Rails会搜索“page.json”,并会抛出错误。
答案 0 :(得分:4)
您需要显式设置格式,内容类型(默认为application/json
)和布局(默认为无):
respond_to do |format|
format.html { render 'page' }
format.json do # render an html page instead of a JSON response
render 'page.html', {
:content_type => 'text/html',
:layout => 'application'
}
end
end
答案 1 :(得分:0)
只需使用完整的文件名。 render 'page.html'
(或调用任何文件)。
请注意,虽然这应该有用,但如果你这样做,你可能会做错事。考虑一下如何重构代码,这样就不必违反规则。