Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove .xml and .json from url when using active resource

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 &lt 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?

like image 462
leejava Avatar asked Apr 20 '09 07:04

leejava


1 Answers

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"
like image 149
Jeremiah Avatar answered Oct 12 '22 09:10

Jeremiah