Search code examples
ruby-on-railsmongoidkaminari

How to add custom URLs


How to add custom URLs like localhost:3000/one_hour/page/2 instead of localhost:3000/one_hour?page=2

to get '/one_hour', to: 'feed_entries#one_hour'

I use Rails 4, Kaminari and mongoid

routes.rb

Rails.application.routes.draw do
  concern :paginatable do
    get '(page/:page)', :action => :index, :on => :collection, :as => ''
  end
  resources :feed_entries, path: 'news', :concerns => :paginatable
  get '/one_hour', to: 'feed_entries#one_hour'
end

feed_entries_controller.rb

class FeedEntriesController < ApplicationController
  one_hour
    @feed_entries = FeedEntry.includes(:source).one_hour.page(params[:page])
  end
end

Solution

  • I found a solution:

    in routes.rb

    get 'one_hour/(page/:page)', controller: 'feed_entries', action: 'one_hour', to: 'feed_entries#one_hour', as: :one_hour