Routes.rb creating problem in rails - ruby-on-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

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.

Rails Haml form url parameter - how to make html form use the desired rails route

With the HAML code:
%form{:action => "activate_foobar", :method => :post, :controller => "foobar", :url => activate_foobar_foobar_index_path}
%input{:type => "submit", :value => "Activate"}
The submit button directs to
No route matches "/activate_foobar"
rather than to "/foobar/activate_foobar". It does not seem to understand the url parameter.
Details
On the index page of foobar, there is a form which I'm trying to post to foobar_controller's method activate_foobar. Foobar does not have a model, as there is no such object - it is only a specialised property of Widget. (Widget has a model, and a method .activate_foobar)
activate_foobar_foobar_index is defined in routes as:
resources :foobar, :only => [:index] do
collection do
post :activate_foobar
end
end
:confirmed with rake:routes to be:
activate_foobar_foobar_index POST /foobar/activate_foobar(.:format) {:action=>"activate_foobar", :controller=>"foobar"}
:as expected.
Furthermore, experimentation with a simple:
=link_to "Activate", activate_foobar_foobar_index_path, {:method => :post}
:routed successfully to /foobar/activate_foobar
Within the confines of using a form (and not using simple_form_for or other model based solutions), how do you correct the path "foobar/activate_foobar"?
Sources:
I'm following http://guides.rubyonrails.org/routing.html "Adding more RESTful Actions" and http://www.w3schools.com/html/html_forms.asp to try to understand how to direct a form to the right method of the right controller.
Like you say it send the form to /activate_foobar, for some reason (maybe haml bug) it don't take the :controller param, to solve this in action write
:action => 'foobar/activate_foobar'
but you must be careful because it takes a relative route instead of absolute. So if you don't want to handle that issue I suggest you to change to rails form helper
= form_tag(activate_foobar_foobar_index_path, :method => :post) do
since activate_foobar is nested under foobar did you try doing
:url => activate_foobar_foobar_index_path(#foobar)
you need to tell rails where to look for the specific foobar to activate_foobar.
hope it helps.

Rails 3 link or button that executes action in controller

In RoR 3, I just want to have a link/button that activates some action/method in the controller. Specifically, if I click on a 'update_specs' link on a page, it should go to 'update_specs' method in my products controller. I've found suggestions to do this on this site:
link_to "Update Specs", :controller => :products, :action => :update_specs
However, I get the following routing error when I click on this link:
Routing Error No route matches {:action=>"update_specs",
:controller=>"products"}
I've read up on routing but I don't understand why I should have to route this method if all other methods are accessible via resources:products.
You need to create a route for it.
For instance:
resources :products do
put :update_specs, :on => :collection
end
Also by default link_to will look for a GET method in your routes. If you want to handle a POST or PUT method you need to specify it by adding {:method => :post } or {:method => :put } as a parameter, like:
link_to "Update Specs", {:controller => :products, :action => :update_specs}, {:method => :put }
Or you can use button_to instead of link_to which handles the POST method by default.

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

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.

Resources