ActiveResource and REST API

Ran into a great problem. REST, as it’s implimented via Rails,
identifies
the URL as a plural in every case. So you would retrieve a single issue
through /issues/. This seems to be technically
incorrect.

I find other (non-Rails) documentation talking about this being singular
for getting a single issue and plural for a list.

And this is also how the site I’m trying to access is configured. Which
means that every time I try to GET a single issue the ActiveResource is
calling for /issues/ instead of the correct
/issue/.

Does anyone know of a way to workaround this problem using
ActiveResource?

Maybe I should ask if anyone else agrees that this is a problem in the
implementation of the REST api in ActiveResource?

Well, I added an inflector to fix this one.
And I had to strip the ‘.json’ by adding this to the model:

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

but I have a lot of associated models to create for this API. Where do
I put something like this to override ALL instances of the
ActiveResource element_path and collection_path calls?

Sounds like you want to look at collection_name on your class.

More here…

http://www.quarkruby.com/2008/3/11/consume-non-rails-style-rest-apis/

Tom A. wrote in post #1070825:

Ran into a great problem. REST, as it’s implimented via Rails,
identifies
the URL as a plural in every case. So you would retrieve a single issue
through /issues/. This seems to be technically
incorrect.

I find other (non-Rails) documentation talking about this being singular
for getting a single issue and plural for a list.

And this is also how the site I’m trying to access is configured. Which
means that every time I try to GET a single issue the ActiveResource is
calling for /issues/ instead of the correct
/issue/.

Does anyone know of a way to workaround this problem using
ActiveResource?

Maybe I should ask if anyone else agrees that this is a problem in the
implementation of the REST api in ActiveResource?

What you express is not a problem. You’re confusing parameters and
actual path segments.
The url in rails is indicative of the controller and action at that
point in the site. At its core rails routing looks something like
map.connect ‘:controller/:action/:id.:format’ actions assigned restful
verbs will figure out on their own path, but custom actions will need
their own path segment.

something like mydomain.com/issues means route to the ‘issues’
controller with no paramaters, which most commonly will go to the index
action.

something like mydomain.com/issues/4 means route to the ‘issues’
controller with parameters {id: 4}. By default mydomain.com/issue/4
would go to a different controller (‘issue’ which would cause naming
conflicts). You can of course get around this by hardcoding resource
paths.

So from there you can figure out where
/publishers/1/magazines/2/photos/3 goes. Publishers controller, get
action, param id:1, magazines controller, get action,…

This should tell you everything you need to know
http://guides.rubyonrails.org/routing.html

Whether or not inflections and pluralizations are a good thing to begin
with, that’s a whole nother thread.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs