问题描述
当我在活动资源中进行映射时,它对 Ruby on Rails 的默认请求总是自动在 url 的末尾添加扩展名.例如:我想通过如下映射从 Ruby on Rails 获取用户资源:
类用户 < ActiveResource::Baseself.site = 'http://localhost:3000'结尾还有一些我需要的东西,我只希望它传递没有扩展名的 url,例如
http://localhost:3000/user
相比之下,它会自动在 url 末尾添加扩展名,如
http://localhost:3000/user.xml
当我从活动资源映射发出请求时,如何省略 url 的扩展名?
起初,我确实使用了 @Joel AZEMAR 的答案,并且在我开始使用 PUT 之前效果很好.执行在 .json/.xml 中添加的 PUT.
一些这里的研究,透露使用 ActiveResource::Base#include_format_in_path
选项对我来说效果更好.
没有 include_format_in_path:
class Foo /foo/1.json"
使用 include_format_in_path:
class Foo /foo/1"
When I do a mapping in active resource, its default request to the Ruby on Rails always automatically add the extension at the end of the url.For example:I want to get user resource from Ruby on Rails by mapping as below:
class user < ActiveResource::Base self.site = 'http://localhost:3000' end
And something that I need, I just want it pass the url without extension like
http://localhost:3000/user
In contrast it automatically adds the extension at the end of the url like
http://localhost:3000/user.xml
How can I omit the extension of the url when I make request from the active resource mapping?
At first, I did use @Joel AZEMAR's answer and it worked quite well until I started using PUT.Doing a PUT added in the .json/.xml.
A bit of research here, revealed that using the ActiveResource::Base#include_format_in_path
option worked far better for me.
Without include_format_in_path:
class Foo < ActiveResource::Base
self.site = 'http://localhost:3000'
end
Foo.element_path(1)
=> "/foo/1.json"
With include_format_in_path:
class Foo < ActiveResource::Base
self.include_format_in_path = false
self.site = 'http://localhost:3000'
end
Foo.element_path(1)
=> "/foo/1"
这篇关于使用活动资源时如何从 url 中删除 .xml 和 .json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!