Hi,

I'm being asked to use this api to retrieve the data because the ror
application won't have any database to store the users information, the
database will only be accessed by this api.

For example for creating a new user I have to send a post request with a
json format to the url: api.server.com/api/v/user/subs

So in my model I have this:

class User << ActiveResource::Base
self.site = "http://api.server.com/api/v"
self.format = :json
end

and when I try this:
u = User.new
u.save

I got 400 Bad request because the request is send to this location

POST http://api.server.com:80/api/v/users.json

I guess the first problem is the rails conventions for pluralize
collections, so it write users instead of user like is needed in the
url, the second problem is that after user I need to and, in this action
of create a new user, the /subs and I don't know how to do this.

After that problem I have another issue in the body of the request it
sends the json like this {"user":{"email":"mail"}}
but the api is expectin something like this {"email":"mail"}


After saying all this I want to know if it is a proper way to make
AResource to behave the way I need or if already exist a plugin/gem to
work with this kind of APIs or if a need to come with a new solution and
leave behind AR

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Arturo drlt at Oct 11, 2012 at 3:09 pm
    So far I came with a solution for two of this issues:


    1) Url using a pluralized name of the object User solved with:
    self.collection_name = "user"
    2) Removing the extension from the URL, I solve this adding a Module
    that I found googling around to override some methods of AR.

    I thought that I had found the solution for another problem, removing
    the element_name from the json adding:

    ActiveResource::Base.include_root_in_json = nil
    But my json still have this node

    {"user":{"email":"hi'\n"}}

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Fernando Almeida at Oct 12, 2012 at 10:37 pm
    There is the remove_root method, is this what you want?

    https://github.com/rails/rails/blob/6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa/activeresource/lib/active_resource/formats.rb#L14


    2012/10/11 arturo drlt <lists@ruby-forum.com>
    So far I came with a solution for two of this issues:


    1) Url using a pluralized name of the object User solved with:
    self.collection_name = "user"
    2) Removing the extension from the URL, I solve this adding a Module
    that I found googling around to override some methods of AR.

    I thought that I had found the solution for another problem, removing
    the element_name from the json adding:

    ActiveResource::Base.include_root_in_json = nil
    But my json still have this node

    {"user":{"email":"hi'\n"}}

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    Fernando Almeida
    www.fernandoalmeida.net

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Fernando Almeida at Oct 13, 2012 at 1:42 am
    Or do you want the ActiveResource::Base.include_root_in_json = false ?

    https://github.com/rails/activeresource/blob/master/test/cases/base_test.rb#L1092


    2012/10/13 Fernando Almeida <fernando@fernandoalmeida.net>
    There is the remove_root method, is this what you want?


    https://github.com/rails/rails/blob/6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa/activeresource/lib/active_resource/formats.rb#L14



    2012/10/11 arturo drlt <lists@ruby-forum.com>
    So far I came with a solution for two of this issues:


    1) Url using a pluralized name of the object User solved with:
    self.collection_name = "user"
    2) Removing the extension from the URL, I solve this adding a Module
    that I found googling around to override some methods of AR.

    I thought that I had found the solution for another problem, removing
    the element_name from the json adding:

    ActiveResource::Base.include_root_in_json = nil
    But my json still have this node

    {"user":{"email":"hi'\n"}}

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    Fernando Almeida
    www.fernandoalmeida.net

    --
    Fernando Almeida
    www.fernandoalmeida.net

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Arturo drlt at Oct 15, 2012 at 4:28 pm

    Xavael wrote in post #1079637:
    Maybe you can use the Custom Methods
    (http://api.rubyonrails.org/classes/ActiveResource/CustomMethods.html)
    to
    try if it works.

    And the try to use the "normal" ActiveResource ...
    Thx for that module it will be very helpful


    Fernando Almeida wrote in post #1079656:
    There is the remove_root method, is this what you want?

    https://github.com/rails/rails/blob/6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa/activeresource/lib/active_resource/formats.rb#L14

    2012/10/11 arturo drlt <lists@ruby-forum.com>
    I thought that I had found the solution for another problem, removing
    Posted via http://www.ruby-forum.com/.
    --
    Fernando Almeida
    www.fernandoalmeida.net
    Yeah I want something like that, with this I think I have all my issues
    solved for now thx guys.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Xavael at Oct 12, 2012 at 7:38 pm
    Maybe you can use the Custom Methods
    (http://api.rubyonrails.org/classes/ActiveResource/CustomMethods.html) to
    try if it works.

    And the try to use the "normal" ActiveResource ...

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/_EEA4JhgBvUJ.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 10, '12 at 10:00p
activeOct 15, '12 at 4:28p
posts6
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase