Re: [Rails] Impossible to get data with ActiveResource

37 views
Skip to first unread message

Colin Law

unread,
Oct 19, 2012, 6:48:53 AM10/19/12
to rubyonra...@googlegroups.com
On 18 October 2012 23:30, chardy <thiba...@gmail.com> wrote:
> Hi,
>
> I'm trying to work with a RESTful server but I have some problem to setup my
> project.
> I have a model class like this :
>
>> class User < ActiveResource::Base
>>
>> self.site =
>> "http://diufvm31.unifr.ch:8090/CyberCoachServer/resources/users/"
>>
>> end
>
>
> this controller :
>
>> class UsersController < ApplicationController
>>
>> def new
>>
>> end
>>
>> def index
>>
>> @users = User.find(:all)
>>
>>
>> respond_to do |format|
>>
>> format.html # index.html.erb
>>
>> format.json { render :json => @users }
>>
>> format.xml { render :xml => @users}
>>
>> end
>>
>> end
>>
>> end
>>
>>
>
>
> and simply this view :
>
>> <% @users.each do |user| %>
>>
>> <p><%= user.username %></p>
>>
>> <% end %>
>>
>>
>
>
> But I can't retrieve the data, @users is empty.
> Anyone has an idea how to fix this issue?

Is there anything useful in log/development.log?

Colin

>
> Thanks
>
> --
> 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 rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/fjonPR37-AQJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

chardy

unread,
Oct 21, 2012, 6:45:28 AM10/21/12
to rubyonra...@googlegroups.com, cla...@googlemail.com
Thanks for your help Colin!

My logs : 

Started GET "/users/index" for 127.0.0.1 at Thu Oct 18 23:01:19 +0200 2012
Processing by UsersController#index as HTML
  Rendered users/index.html.erb within layouts/application (0.6ms)
Completed 500 Internal Server Error in 140ms

ActionView::Template::Error (undefined method `each' for nil:NilClass):
    1: <% @users.each do |user| %>
    2: <h1><%= user.username %></h1>
    3: 
    4: <% end %>
  app/views/users/index.html.erb:1:in `_app_views_users_index_html_erb___162613411_2191123680'

I think the problem is the Error 500...? It's strange because if you try the link in the browser it's works... 
Thanks for your help!

Colin Law

unread,
Oct 21, 2012, 8:27:29 AM10/21/12
to rubyonra...@googlegroups.com
The 500 error is not the problem, that is just the result of @users being nil.

I think you need to enable logging so that you can see what
activeresource is doing. I have not used activeresource but a quick
google found [1] which looks useful and has a section Logging
Requests. Hopefully you will then see what is going on.

Colin

[1] http://ofps.oreilly.com/titles/9780596521424/activeresource_id59243.html

chardy

unread,
Oct 21, 2012, 10:06:03 AM10/21/12
to rubyonra...@googlegroups.com, cla...@googlemail.com
Yes, good idea, now I can understand where is the problem : 


The server is not configured to respond for .xml (or .json). The content type should be defined in the header...
Is it possible to setup this in ruby...?

Thanks a lot!

Hassan Schroeder

unread,
Oct 21, 2012, 10:19:08 AM10/21/12
to rubyonra...@googlegroups.com
On Sun, Oct 21, 2012 at 7:06 AM, chardy <thiba...@gmail.com> wrote:

> GET http://diufvm31.unifr.ch:8090/CyberCoachServer/resources/users/users.xml
> --> 404 Not Found 32 (129ms)
>
> The server is not configured to respond for .xml (or .json).

If this server isn't under your control, shouldn't you have some kind
of documentation on what it does -- what requests it responds to and
in what format?

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

chardy

unread,
Oct 21, 2012, 10:44:15 AM10/21/12
to rubyonra...@googlegroups.com

Yes, it's for a project in my university. I know that the we need to specify the format in the header and the server can handle json and xml format.

Here is the documentation :

Read only resources:

- SportsResource / - Get list of all sports [GET, public]

- UsersResource /users/ - Get list of all users [GET, public]

- PartnershipsResource /partnerships/ - Get list of all partnerships [GET, public]

- SportResource /sports/{sportname} - Get sport entity and its list of subscriptions [GET, public]

"Public" write resources:

- UserResource /users/{username} - Get user entity [GET, public*] - Create new user [PUT, public]

- Update user profile [PUT, private] - Delete user [DELETE, private]

Private write resources:

- PartnershipResource /partnerships/{username1};{username2}/ - Get partnership entity [GET, public*]
- Propose (or confirm** proposed) partnership [PUT, private]

- Delete (or decline** proposed) partnership [DELETE, private]

- SubscriptionResource /users/{username}/{sportname} - Get subscription entity [GET, public*] /partnerships/{username1};{username2}/{sportname}/ - Subscribe (as user or partnership) to a sport [PUT, private]

- Update subscription [PUT, private]
- Submit new entry [POST, private]
- Delete subscription [DELETE, private]

- EntryResource /[uri of subscription]/{entityid} - Get entry entity [GET, public*] - Update entry data [PUT, private]

- Delete entry [DELETE, private]

* = Read accessability depends on the privacy settings for the respective entities ** = A partnership is created/deleted in the following way:

  • PUT by user1 proposes a new partnership. PUT by user2 confirmes the proposal and the partnership becomes "operational"

  • DELETE on an operational partnership results in a proposed partnership. DELETE on a proposed partnership deletes it.


Colin Law

unread,
Oct 21, 2012, 10:51:06 AM10/21/12
to rubyonra...@googlegroups.com
On 21 October 2012 15:06, chardy <thiba...@gmail.com> wrote:
> Yes, good idea, now I can understand where is the problem :
>
> GET http://diufvm31.unifr.ch:8090/CyberCoachServer/resources/users/users.xml

Is the url supposed to be resources/users/users.xml? Perhaps it
should just be resources/users.xml.

Colin

Hassan Schroeder

unread,
Oct 21, 2012, 11:33:08 AM10/21/12
to rubyonra...@googlegroups.com
On Sun, Oct 21, 2012 at 7:44 AM, chardy <thiba...@gmail.com> wrote:

> - UsersResource /users/ - Get list of all users [GET, public]

Not much for "documentation" :-/

http://diufvm31.unifr.ch:8090/CyberCoachServer/resources/users

returns an HTML page with a paginated listing of users, but that's
hardly what you want for ActiveResource.

If no more info on this server is available, I'd probably poke at it a bit
with wget or curl to see if I could get a useful response.

Good luck!

chardy

unread,
Oct 21, 2012, 4:10:48 PM10/21/12
to rubyonra...@googlegroups.com, cla...@googlemail.com
Yes, sorry I made a mistake it's only users.xml but the problem is the same...
Is it possible to retrieve the data without the extension .xml but just specify the type in the header?

I have not found anything on the web...

chardy

unread,
Oct 21, 2012, 4:53:53 PM10/21/12
to rubyonra...@googlegroups.com, cla...@googlemail.com
So, I tried to overwrite the methode collection_path :
def collection_path(prefix_options = {}, query_options = nil)
        check_prefix_options(prefix_options)
        prefix_options, query_options = split_options(prefix_options) if query_options.nil?
        "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
      end
by
def collection_path(prefix_options = {}, query_options = nil)
        check_prefix_options(prefix_options)
        prefix_options, query_options = split_options(prefix_options) if query_options.nil?
        "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
      end

And now it's OK :)

...But :(
I have an other error now :
NoMethodError (undefined method `collect!' for #<Hash:0x1030aeed0>):
app/controllers/users_controller.rb:6:in `index'

Anyone has an idea

Thanks a lot for your support! 

Colin Law

unread,
Oct 21, 2012, 5:05:14 PM10/21/12
to rubyonra...@googlegroups.com
What do you think that error might mean?

Colin

chardy

unread,
Oct 21, 2012, 5:17:28 PM10/21/12
to rubyonra...@googlegroups.com, cla...@googlemail.com
It's finally seems to work :)
My ActiveResource model looks like this :

class User < ActiveResource::Base
  class << self
    def element_path(id, prefix_options = {}, query_options = nil)
      check_prefix_options(prefix_options)
      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

    def collection_path(prefix_options = {}, query_options = nil)
      check_prefix_options(prefix_options)
      prefix_options, query_options = split_options(prefix_options) if query_options.nil?
      "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
    end
    def instantiate_collection(collection, prefix_options = {})
      collection = collection[element_name.pluralize] if collection.instance_of?(Hash)
      collection.collect! { |record| instantiate_record(record, prefix_options) }
    end
  end
end

I founded how to solve the last error here : https://github.com/rails/rails/issues/2318#issuecomment-3555973

Thanks a lot for your support! :)
Reply all
Reply to author
Forward
0 new messages