2

just getting started with Rails, I would like to consume a webservice (using ActiveResource) that has the following endpoint:

GET /user?some_header=XYZ

This is my ActiveResource Class:

    class User < ActiveResource::Base
      self.site = "url"
      set_collection_name 'user'  #avoid pluralization within the url
    end

How would a call for the above endpoint now look like?

I tried

    User.get('', headers={:some_header => "XYZ"})

but I'm getting a 404 (the request works when I fire it by hand).

zero-divisor
  • 560
  • 3
  • 19

2 Answers2

1

Try

User.all(:params=>{:some_header => "XYZ"})

If you want to avoid format in your path, you can ovveride base collection_path method

   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}.#{format.extension}#{query_string(query_options)}"
    end

to

   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
Fivell
  • 11,829
  • 3
  • 61
  • 99
  • Tried it, getting an strange error **undefined method `collect!' for #** . When I look at the log file it seems that the right request is constructed though. – zero-divisor Feb 15 '12 at 12:39
1

Found the problem. Although I specified an empty path in my get method, Rails internally appended ".json", so the constructed URL was

/user/.json?some_header=XYZ"

The way I fixed it was

     User.collection_name = ""
     User.get('user', headers={:some_header => "XYZ"})

Anyone knows a cleaner solution?

zero-divisor
  • 560
  • 3
  • 19