Activeresource usage in Ruby on Rails _ruby topics

Source: Internet
Author: User
Tags extend http request ruby on rails

When an HTTP response is a format that is different from the one in which it exists (XML and JSON), some additional formatting needs to be parsed, creating a format that you are accustomed to, and using it in a category. Idiomatic formats should be implemented in the following ways: 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)
       # Data encoded in a new format and returned
      End

      def decode (CSV)
       # data is decoded in a new format and returns the end-end-end

  class User < activeresource::base
   Self.format = Activeresource::formats::extend::csvformat

   ...
  End

If the HTTP request should not extend the send, overwrite the activeresource::base Element_path and Collection_path method, 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_op Tions.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 there is any need to change the URL.

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.