[英]how can I generate json from respond_to method in rails?
If I have a block of code like this: 如果我有这样的代码块:
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
How do I add something like 我该如何添加类似的内容
format.json
Any tips, pointers, ideas gladly welcomed... 任何提示,指示,想法都欢迎...
It's just like the other formats except that you use render :json
instead. 它就像其他格式一样,除了你使用render :json
。
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
format.json { render :json => @post }
end
or you can handle it as javascript 或者你可以用javascript来处理它
respond_to do |format|
format.js { render :json { :only => :name }.to_json }
end
then you just access your action with ".js" in the end. 然后你最后用“.js”访问你的动作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.