Search code examples
ruby-on-railsruby-on-rails-4devisewill-paginate

will_paginate confusing urls with devise authenticated root routes


I have the following in my routes.rb:

...
authenticated :user do
    root to: 'game_shelves#index', as: :authenticated_root
end
unauthenticated do
    root to: 'editions#index'
end
...
resources :editions, :path => "games" do
    collection do
        get 'to_review'
        post 'review'
        get 'existing_work'
    end
    member do
        put 'split'
    end
    resources :expansions do
    end
end
...

will_paginate is like this:

<%= will_paginate @collection, renderer: BootstrapPagination::Rails %>

And the result links are like this:

http://localhost:3000/?page=2

If I make unauthenticated root be other thing than editions#index, the links are correct, like this:

http://localhost:3000/games?page=2

I've tried using this:

<%= will_paginate @collection, :params => {:controller => '/editions', :action => 'index'}, renderer: BootstrapPagination::Rails %>

But it does not force will_paginate to use the right route. And if I try this:

<%= will_paginate @collection, :params => {:controller => editions_path }, renderer: BootstrapPagination::Rails %>

Rails give me route errors:

No route matches {:action=>"index", :controller=>"games", :page=>2}


Solution

  • I've inverted my routes.rb and it works now. It seems order is important and I was not aware.

    ...
    resources :editions, :path => "games" do
        collection do
            get 'to_review'
            post 'review'
            get 'existing_work'
        end
        member do
            put 'split'
        end
        resources :expansions do
        end
    end
    authenticated :user do
        root to: 'game_shelves#index', as: :authenticated_root
    end
    unauthenticated do
        root to: 'editions#index'
    end