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?

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 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/-/fjonPR37-AQJ.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Colin Law at Oct 19, 2012 at 10:49 am

    On 18 October 2012 23:30, chardy 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 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/-/fjonPR37-AQJ.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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.
  • Chardy at Oct 21, 2012 at 10:53 am
    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!

    --
    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/-/35Kf2uqK6fAJ.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Colin Law at Oct 21, 2012 at 12:28 pm

    On 21 October 2012 11:45, chardy wrote:
    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...
    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

    --
    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.
  • Chardy at Oct 21, 2012 at 2:14 pm
    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!

    --
    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/-/78t00Qz9Sh0J.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Hassan Schroeder at Oct 21, 2012 at 2:19 pm

    On Sun, Oct 21, 2012 at 7:06 AM, chardy 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.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    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.
  • Chardy at Oct 21, 2012 at 2:44 pm
    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.




    --
    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/-/Im1iaQuuEIIJ.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Hassan Schroeder at Oct 21, 2012 at 3:33 pm

    On Sun, Oct 21, 2012 at 7:44 AM, chardy 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!
    --
    Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    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.
  • Colin Law at Oct 21, 2012 at 2:51 pm

    On 21 October 2012 15:06, chardy 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
    --> 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!
    --
    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.
  • Chardy at Oct 21, 2012 at 8:10 pm
    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...

    --
    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/-/n5AO_bPa2roJ.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chardy at Oct 21, 2012 at 8:54 pm
    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 :)
    *GET http://diufvm31.unifr.ch:8090/CyberCoachServer/resources/users
    --> 200 OK 1260 (1735.7ms)
    *
    *
    *
    ...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!

    --
    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/-/UFNEBhzjJJUJ.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Colin Law at Oct 21, 2012 at 9:13 pm

    On 21 October 2012 21:53, chardy wrote:
    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 :)
    GET http://diufvm31.unifr.ch:8090/CyberCoachServer/resources/users
    --> 200 OK 1260 (1735.7ms)

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

    Colin

    --
    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.
  • Chardy at Oct 21, 2012 at 9:17 pm
    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
    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! :)

    --
    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/-/2t_fGEWP-dYJ.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 19, '12 at 10:49a
activeOct 21, '12 at 9:17p
posts13
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase