Re: Problem with the pagination example

44 views
Skip to first unread message

Nick Sutterer

unread,
Mar 29, 2013, 12:23:51 AM3/29/13
to roar...@googlegroups.com
That must be a Rails routing issue then. It is needed to compute the
pagination link, I guess you're using URL helpers in your representer.
Can you paste the representer module here, please?

On Tue, Mar 26, 2013 at 11:20 AM, Marcel Augsburger
<maugs...@gmail.com> wrote:
> First of all, congrats for the awesome work. I'm working on parliamentary
> monitoring tools, and wasn't sure how to handle the interaction between the
> back end and multiple front ends with different fields, and it looks like
> ROAR is just what I needed!
>
> The first thing I did was cloning elvanja's roar_example, and it helped a
> ton! Then I needed pagination, and tried to follow the Pagination With ROAR
> tutorial with no success... I cloned the ruby-on-rest project, but still had
> problems with that.
>n
> This is what happens when working with Rails 3.2.1, and will_paginate 3.0.3:
>
> I create a bowl, and paginate the fruits. Everything working: page.to_json
> returns fine. Then I extend BowlPageRepresenter and .to_json gives me this
> error:
>
> ActionController::RoutingError: No route matches {:action=>”show”,
> :controller=>”bowls”, :page=>page 1}
>
> This might be a noob question, but, why is it routing anyway?
> Other methods like to_xml work fine… probably because they’re not overridden
> by ROAR like to_json. Mixed-in methods like .page_url are also giving me
> similar errors.
>
> Any clues? Thanks again!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Roar" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to roar-talk+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Message has been deleted

Nick Sutterer

unread,
Apr 20, 2013, 12:48:13 AM4/20/13
to roar...@googlegroups.com
Did you try to create this route? I don't understand why it says :page=>page 1 - what's the matter with that "page 1"? Shouldn't that simply be "1"?


On Tue, Apr 2, 2013 at 7:44 AM, Marcel Augsburger <maugs...@gmail.com> wrote:
It's the same one as the ruby-on-rest example:

------------ Fruit Representer --------------
module FruitRepresenter
  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia
  property :title
  link :self do
    fruit_url(title)
  end
end

--------------- Bowl Representer --------------
module BowlRepresenter
  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia
  property :location
  collection :fruits, :class => Fruit, :extend => FruitRepresenter
  link :self do
  end
end

----------- BowlPage Representer ------------
module PaginationRepresenter
  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia
  property :total_entries
  link :self do |opts|
    page_url(opts[:model], :page => current_page)
  end
  link :next do |opts|
    page_url(opts[:model], :page => next_page) if next_page
  end
  link :previous do |opts|
    page_url(opts[:model], :page => previous_page) if previous_page
  end
  def items
    self
  end
  def page_url(*)
    raise "Implement me."
  end
end
module BowlPageRepresenter
  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia
  include PaginationRepresenter
  collection :items, :extend => FruitRepresenter
  def page_url(*args)
    bowl_url(*args)
  end
end


I clone the project and execute the following on rails console:
>>fruits = Fruit.all
=> [#<Fruit id: 1, title: "Apple", created_at: "2012-03-02 13:37:57", updated_at: "2012-03-02 13:37:57">, #<Fruit id: 2, title: "Orange"...

>>bowl = Bowl.new
=> #<Bowl id: nil, location: nil, created_at: nil, updated_at: nil>

>>bowl.fruits = fruits
 => [#<Fruit id: 1, title: "Apple", created_at: "2012-03-02 13:37:57", updated_at: "2012-03-02 13:37:57">, #<Fruit id: 2, title: "Orange"...

>>bowl.save
(0.1ms)  begin transaction
  SQL (59.6ms)  INSERT INTO "bowls" ("created_at", "location", "updated_at") VALUES (?, ?, ?)...

>>page = bowl.fruits.paginate(:page=> 1, :per_page=>2)
Fruit Load (0.3ms)  SELECT "fruits".* FROM "fruits" INNER JOIN "bowls_fruits"...

>>page.to_json
=> "[{\"created_at\":\"2012-03-02T13:37:57Z\",\"id\":1,\"title\":\"Apple\",\"updated_at\":\"2012-03-02T13:37:57Z\"}...

>>page.extend(BowlPageRepresenter)
=> [#<Fruit id: 1, title: "Apple", created_at: "2012-03-02 13:37:57", updated_at: "2012-03-02 13:37:57">...

>>page.to_json(:bowl => @bowl)
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"bowls", :page=>page 1}

I'm not sure... should I create a route like that? I tried, but maybe I'm not getting it right. Thanks again!

Marcel Augsburger

unread,
Apr 23, 2013, 10:16:36 AM4/23/13
to roar...@googlegroups.com
Magic happened! Everything works now! I cloned the project again and bundle installed gems, and it works!
The versions that gave me the problem were:
representable 1.1.7
roar 0.10.2
roar-rails 0.0.5

And the ones that work are:
representable 1.4.0
roar 0.11.14
roar-rails 0.0.13

And by the way, in my previous email, when I wrote page.to_json(:bowl => @bowl), I meant page.to_json(:model => @bowl). It obviously failed with the previous, but it gave me the same error with the latter, until the new gem versions :).

Thanks again!


2013/4/20 Nick Sutterer <apot...@gmail.com>

Nick Sutterer

unread,
Apr 23, 2013, 8:16:23 PM4/23/13
to roar...@googlegroups.com
Wonderful! See how updates can help? :-P

Marcel Augsburger

unread,
Apr 23, 2013, 8:33:41 PM4/23/13
to roar...@googlegroups.com
Update every day from now on ;)


2013/4/23 Nick Sutterer <apot...@gmail.com>

Nick Sutterer

unread,
Apr 23, 2013, 9:16:54 PM4/23/13
to roar...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages