Impossible to get data with ActiveResource

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/&quot;

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 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!

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] Online Feedback Publishing System - O'Reilly Media

Yes, good idea, now I can understand where is the problem :

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). The content type should be defined in the header…

Is it possible to setup this in ruby…?

Thanks a lot!

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?

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.

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

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!

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…

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 :slight_smile:

GET http://diufvm31.unifr.ch:8090/CyberCoachServer/resources/users → 200 OK 1260 (1735.7ms)

…But :frowning:

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!

What do you think that error might mean?

Colin

It’s finally seems to work :slight_smile:

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

self.site = “http://diufvm31.unifr.ch:8090/CyberCoachServer/resources/

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! :slight_smile: