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


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




How to keep data when run test in rails

1:



How can I modify Rails' config within an engine plugin?
At first, I did use @Joel AZEMAR's answer and it worked quite well until I started using PUT.


Rails nested resource issue
Doing a PUT added in the .json/.xml.


How do I install rails on a Windows machine that uses a proxy server to get to the internet?
.
What is the best way to implement user login in Rails?
A bit of research here, revealed that using the ActiveResource::Base#include_format_in_path option worked far better for me.


Single Table Inheritance (STI) column associations
.
Trackback implementation: rel=“trackback” vs RDF
Without include_format_in_path:.
How best to sanitize rich html with rails?
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" 


2:


You can override methods of ActiveResource::Base. Add this lib in /lib/active_resource/extend/ directory don't forget uncomment
"config.autoload_paths += %W(#{config.root}/lib)" in config/application.rb.
module ActiveResource #:nodoc:   module Extend     module WithoutExtension       module ClassMethods         def element_path_with_extension(*args)           element_path_without_extension(*args).gsub(/.json|.xml/,'')         end         def new_element_path_with_extension(*args)           new_element_path_without_extension(*args).gsub(/.json|.xml/,'')         end         def collection_path_with_extension(*args)           collection_path_without_extension(*args).gsub(/.json|.xml/,'')         end       end        def self.included(base)         base.class_eval do           extend ClassMethods           class << self             alias_method_chain :element_path, :extension             alias_method_chain :new_element_path, :extension             alias_method_chain :collection_path, :extension           end         end       end       end   end   end 
in model .
class MyModel < ActiveResource::Base   include ActiveResource::Extend::WithoutExtension end 


3:


You can do this by overriding two ActiveResource's methods in your class:.
class User < ActiveResource::Base   class << self     def element_path(id, prefix_options = {}, query_options = nil)       prefix_options, query_options = split_options(prefix_options) if query_options.nil?       "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"     end      def collection_path(prefix_options = {}, query_options = nil)       prefix_options, query_options = split_options(prefix_options) if query_options.nil?       "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"     end   end    self.site = 'http://localhost:3000' end 



81 out of 100 based on 51 user ratings 201 reviews

@