我正在使用 Rails 3 并且我正在使用这个新的 Rails 3 东西,它可以用 response_to 和 respond_with 来做,所以我的 Controller 有这样的东西:
http://davidwparker.com/2010/03/09/api-in-rails-respond-to-and-respond-with/
respont_to :json
def show
@organization = Organization.includes([:tableNamesBlah]).find(params[:id])
respond_with(@organization)
end
我将它传递给 JBUILDER 的 View ,....
所以在 URL 如果我去
http://localhost:3000/manager/1.json
它工作正常并以 JSON 格式返回数据,但如果我转到相同的 URI 而不指定 .json 作为它的结尾,那么它不会返回任何内容。
如何以一种可以查看数据但不需要在 URL 末尾键入 .json 的方式修改我的代码?
这是我应该在 Controller 中做的事情吗?或者我应该改变 JBUILDER 处理数据的方式还是与路由有关?
如果您需要查看代码的其他部分,请告诉我
最佳答案
如果你想设置默认格式,你可以在你的 routes.rb 文件中设置它:
resources :things, defaults: { format: :json }
这应该给你你想要的行为(如果我正确理解你的问题):
/things/1 # returns json
/things/1.json # returns json
/things/1.html # returns html
关于ruby-on-rails - 如何不将格式( json, html, xml )传递给 REST URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14885991/