will_paginate with resources route collection - ruby-on-rails

Can't quite figure out what's going on here. So in my routes I have
sso.namespace(:admin) do |admin|
admin.resources :locations, :collection => {:search => :post}
Generating the pagination just fine on the view. Here's my view code:
<%= will_paginate #search_locations, :class => "loc_pagination", :params => {:controller => 'sso/admin/locations', :action => 'search'}, :style => "text-align: center;" if #search_locations %>
Problem is that when I click on the links, it fires a GET request and sticks search in the params. Here is the parameters in the server log.
Parameters: {"action"=>"show", "id"=>"search", "page"=>"2", "controller"=>"sso/admin/locations"}
The generated html code looks sound, but I can't figure out what it's doing wrong.

You've configured the search action to only respond to post requests. When you attempt to visit the second page of the search results (via a get request) the show route picks up the response. Try changing the :collection => { :search => :any } and append the search term to the params passed to will_paginate.

Related

Routing and form in Rails

I have a controller for a resource, BuddiesController. My routes config file up until now has been
resources :buddies
match ':controller(/:action(/:id))', :via => [:get, :post]
I didn't realize what the ' resources :buddies ' line was doing until I read up on routing in Rails just now, because the behavior has been identical with what I expected until now. The problem was that I wanted to add a non-CRUD action to the controller: 'search'. Every time I used link_to(:action => 'search'), I would get an exception saying that action 'show' could not be found despite the url being ' localhost:3000/buddies/search ' as expected. I have several questions arising from this:
Firstly, the form I used in 'new' stopped working:
%= form_for(#buddy, {:action => :create, :method => :post, :html => {:role => "form"}}) do |f| %>
because buddies_path couldn't be found. How could I manually add a buddies_path to my routes?
Secondly, I revised the form to use:
<%= form_for(#buddy, :url => {:action => :create, :id => #buddy.id}, :html => {:role => "form", :id => #buddy.id}) do |f| %>
but this has caused the form to give me password and email confirmation not matching errors even if they match. What's going on here?
Lastly, what is the best way to add a search action to my resource?
#routes.rb
resources :buddies
collection do
get :search
end
end
now when you run rake routes | grep 'buddies' you will get output something like this :
now you need to define this search action in your buddies controller .
#buddies_controller.rb
Class BuddiesController < ApplicationController
def search
end
end
Have your search form in app/views/buddies/search.html.erb
Now in order to open your search form / to hit your search action you need to use
<%= link_to 'Search XYZ', search_buddies_path %>
against buddies#search you can see search_buddies
In routes.rb:
resources :buddies do
collection do
post :search
end
end
This might make your routing works.

Can I make a route that does not map to a URL in Ruby on Rails?

I have a bunch of routes in my routes.rb that look like this:
post 'events/form1'=> 'events#form1', :as => 'events_form1'
post 'events/form2'=> 'events#form2', :as => 'events_form2'
post 'events/form3'=> 'events#form3', :as => 'events_form3'
These controller actions process data from my forms. The user is never going to need to access these actions by URL so I wanted to do something like this:
post 'events#form1'
and then, in my form, write
<%= simple_form_for :something, :url => url_for{:controller => 'events', :action => 'form1'}, :method => "post" do |f| %>
This doesn't work because rails complains that the route is invalid. Is it possible to have a route without a url? If not, how can I clean up my routes file?
The simple_form_for call still needs a URL, even if your users will never use it directly.
You can kind of see this in the code itself, in that one of the options you're sending to simple_form_for is an option for the URL to use:
:url => url_for{:controller => 'events', :action => 'form1'}

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 %>

Rails Routing - Not showing an action

I have two routes:
map.all_devices '/all_devices', :controller => 'all_devices', :action => 'initialize_devices'
map.show_user_date_select '/all_devices', :controller => 'all_devices', :action => 'show_user_date_select'
I want a user to click on a button, do the show_user_date_select action then be redirect back to mysite.com/all_devices. The route above does work, but always sends the user to initialize_devices, which resets the 'show_user_date_select' action.
Looks like you mapped both of those to the same route. Since you put the initialize_devices one on top, it renders that one with higher priority, which is why you always get that.
Probably what you want is something like this in the routing:
map.all_devices '/all_devices', :controller => 'all_devices', :action => 'index'
Then have a different route which the form submits to, such as /all_devices/show_user_date_select, and redirect to /all_devices afterwards:
def show_user_date_select
# do stuff here
redirect_to all_devices
end
The routes seem a little odd to me. Try something like:
map.resources :all_devices, :member => { :all_devices => :get, :show_user_date_select => :get }
Then in your views:
<%= link_to "All Devices", path_to_all_devices_all_devices %>
<%= link_to "Show Dates", path_to_show_user_date_select_all_devices %>
The link names are awful, but should work with your existing app. I would rather see:
map.resources :devices, :member => { :all => :get, :select => :get }
<%= link_to "All Devices", path_to_all_devices %>
<%= link_to "Show Dates", path_to_select_devices %>
But that will require a bit or re-plumbing on your part.
It looks like that route explicitly maps /all_devices to the initialize_devices action.
Perhaps you should set some piece of session-specific information the first time initialize_devices is reached and does nothing on subsequent requests.

Routes.rb creating problem in rails

Im using a link which is
:communities,:action=>:usergroups} , :class => "adminbutton viewusergrp" %>
and routes contain the
map.resources :vccommunities,:member => {:usergroups => :get}
and some more action names are also specified here in member and collection.
Im taking care of alphabetical order while listing these actions within :member
But while clicking on that link ...it is taking :action => show, :id => usergroups,
:controller => :communities
so it is thowing error ,since it is not going to the proper action in the controller.
Please help me in that.
the problem is not with your route problem is with your link. as per the defination of routes rails internally searches for id in link hash in your eg link like this
<%= link_to 'View User Group', {:controller=>:communities,:action=>:usergroups, :id => 20} , :class => "adminbutton viewusergrp" %>
should work cause this will map to
/communities/20/usergroups
for further refrence can be taken from this page

Resources