Defining pretty urls for Rails 4 and will_paginate - ruby-on-rails

I'm using will_paginate for my home page of the application which is the home action of the static_pages controller.
root 'static_pages#home'
The will_paginate URLs are as follows
http://localhost:3000/static_pages/home?page=3
I would prefer to take out the controller name in there and have it look like
http://localhost:3000/home?page=3
Or even something prettier like
http://localhost:3000/home/3
My pagination looks like this
will_paginate #product_feed_items, :page_links => false, :previous_label => "Newer", :next_label => "Older"
and I have tried a couple things like this with no luck
will_paginate(#product_feed_items, :params => { :controller => "static_pages", :action => "home" }, :page_links => false, :previous_label => "Newer", :next_label => "Older")
Does anything have a good solution for this??

If you are looking to do this only for few actions try this:
get "home(/:page)" => "static_pages#home"

You will need to do this via the routes.rb file. Have a look at the routing guide here: http://guides.rubyonrails.org/routing.html
Specifically section 3 on non-resourceful routes.

Related

Paginating with kaminari on a custom route

I'm having a problem with kaminari, it doesn't seem to be able to paginate results when using a custom "unRestful" url.
resources :provinces, :path => '', :only => [ :show ] do
resources :sports, :only => [ :show ] do
match ':page', :controller => 'facilities', :action => 'index'
end
end
So, /:foo/sports/:bar points to a controller, /:foo/sports/:bar/1 points to another 1. It's a disgusting URL scheme but I don't have the leverage right now to change the specs.
If I call the page without Kaminari, everything works as expected, I see the first page. When I use Kaminari like:
<%= paginate #facilities, :params => { :controller => 'facilities', :action => 'index' } %>
Rails gives me a routing error on the following URL:
http://lvh.me:8080/milano/sports/palestra/1
No route matches {:controller=>"facilities", :province_id=>"milano", :sport_id=>"palestra", :page=>nil}
I honestly don't know what to do, everything seems right to me, and couldn't find a kaminari group or more documentation on my case.
The problem seems to be that the call to paginate somewhat generates a URL with page set to nil...
Any suggestion?
Using rails (3.2.8)
Using kaminari (0.14.0)
in controller
#facilities = kaminari.paginate_array(#facilities).page(params[:page]).per(params[:per_page])
and in your view <%= page_entries_info #facilities %> & <%= paginate #facilities %>

Why to add a connection in routes file when using link_to in rails 2

I was trying to accomplish the following:
<%= link_to "Log out", { :controller
=> 'users', :action => 'logout' }, :class => 'menulink2' %>
But it didn't work, it always redirected me to a show view. I had to had the following to my routes.rb:
map.connect 'users/logout',
:controller => 'users', :action =>
'logout'
Why didn't rails recognize the action I was passing ('logout') ?
That logic has to be specified somewhere. There's got to be some mapping from the hash {:controller => 'users', :action => 'logout'} to a url, and the place that's done in rails is the routes.rb file. In older versions of rails many routes.rb came with a default at the end:
map.connect ':controller(/:action/(:id(.:format)))'
Which would make it so that most any :controller, :action hash could be specified and then routed to host.url/:controller/:action.
With the more modern versions resource-based routes are heavily favored, and controllers which don't follow rails' REST conventions (i.e. having only :index,:show,:create,:new,:edit,:update,:destroy methods) generally have to have their routes explicitly specified in some way.
(Either with map.resources :users, :collection => {:get => :logout} or with map.connect( 'some_url', :controller => 'users', :action => 'logout'}))
I'm guessing, but the reason they did that is probably that the actions of a controller are really just its public methods.
It's frequently nice to have public methods in your controllers that aren't url-end-points for testing purposes.
For instance, you could have before_filters as public methods that you might want to test without having to use #controller.send(:your_before_filter_method) in your test code.
So they whitelist the resource actions, and make the others unreachable by default. Let me look through the rails changelog and see if I'm right.

Custom Routing in Rails

I've looked around for a while now, but I'm not sure how best to describe my request to google, so thought I'd ask here ;)
In rails, I know that when you nest restful routes, you generally get something like:
http://localhost/categories/1/articles/2
If you want something more meaningful, you can use slugs or friendly_id to get something like
http://localhost/categories/all-your-needs/articles/rock-out-with-this-article
(assuming you have unique names).
My question is, how can I remove the controller from the url rewriter so you get something like:
http://localhost/all-your-needs/rock-out-with-this-article
Is this possible?
Yes it is. You can use something like this:
Rails 2:
map.show_article ':category/:article', :controller => "articles", :action => "show"
Edit:
Ok. Here you have the urls for the other REST actions:
map.edit_article ':category/:article/edit', :controller => "articles", :action => "edit".
For update add :conditions => { :method => :post } to the previous one.
For delete, use the first one with :conditions => { :method => :delete }.
For new and create, you can use:
map.new_article ':category/new', :controller => "articles", :action => "new"
and for create the same but with :conditions => { :method => :post }. I hope I've been able to help you!

Rails 3 routing - passing params from routes.rb

In rails 2.3.5 you could do something like this inside the routes.rb file:
map.root :controller => "pages", :action => "show", :id => 3
In rails 3 I haven't found any way to pass a specific parameter (like in rails 2.3.5 with :id => 3).
I know I can handle it from the controller and have the same result (which I did), but I was wondering if there is a way to do the same thing in rails 3 from the routes.rb or has it changed because it is better practice for some reason?
Are you sure the following doesn't work?
root :to => "pages#show", :id => 3

RoR: I'm having trouble doing a simple link to an action

I am trying to link to action addData in the entries controller. I have constructed the link like this:
<%= link_to image_tag (w.link, :border =>0) ,:controller => :entries, :action => :addData %>
but when I click on the link, I get this error:
Couldn't find Entry with ID=addData
I'm pretty sure this is because I have a restful design. Is there a way around this problem? Thanks for reading.
Rails has migrated wholly to a RESTful design. This means that in order to use non standard actions you have to add them to your resources in config/routes.rb.
If they operate on all resources you add them to the hash :collection => {:addData => :post}
In case you have one operating on a single resource with an id use :member.
To some it up for you.
map.resources :entries, :collection => {:addData => :post}
To use the old style of mapping any action to any controller you can add the following two lines to your config/routes.rb
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
Have you defined the route properly for this action addData?
By the way try this :
<%= link_to image_tag (w.link, :border =>0) ,{:controller => :entries, :action => :addData} %>

Resources