使用活动资源时如何从 url 中删除 .xml 和 .json [英] How to remove .xml and .json from url when using active resource

查看:20
本文介绍了使用活动资源时如何从 url 中删除 .xml 和 .json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在活动资源中进行映射时,它对 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 <活动资​​源::基础self.site = 'http://localhost:3000'结尾Foo.element_path(1)=>/foo/1.json"

使用 include_format_in_path:

class Foo <活动资​​源::基础self.include_format_in_path = falseself.site = 'http://localhost:3000'结尾Foo.element_path(1)=>/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆