ActiveResource in Ruby on Rails

Source: Internet
Author: User
Tags ruby on rails

ActiveResource in Ruby on Rails

When an HTTP response is in a different format (XML and JSON) than an existing one, you need to parse some additional formats to create a common format and use it in the category. The following methods should be implemented in the conventional format: extension, mime_type,
Encode and decode.

Module ActiveResource module Formats module Extend module CSVFormat extend self def extension 'csv' end def mime_type 'text/csv' end def encode (hash, options = nil) # The data is encoded in the new format and the end def decode (csv) is returned. # The data is decoded in the new format and the end class User is returned. <ActiveResource: Base self. format = ActiveResource: Formats: Extend: CSVFormat... end

If the HTTP request should not be extended, overwrite the ActiveResource: Base element_path and collection_path methods, and remove the extended part.

  class User < ActiveResource::Base   ...   def self.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   def self.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}/#{URI.parser.escape id.to_s}#{query_string(query_options)}"   end  end

These methods can also be overwritten if you need to modify the website.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.