I need a bit of help with converting routing from Rails 2 to Rails 3.
In app/views/layouts/application.html.erb, I have:
<%= link_to "Reports", reports_path %><br>
There is a ReportsController, and in app/views/reports/index.html.erb, I have this:
<%= link_to "Clients With Animals", :action => "getAnimals", :controller => "clients" %>
Then, in config/routes.rb, I have this (Rails 3)
match '/reports' => "reports#index"
match '/clients/getAnimals', to: "clients#getAnimals"
I get this error when I click on the "getAnimals" link on the reports page:
ActiveRecord::RecordNotFound in ClientsController#show
Couldn't find Client with id=getAnimals
I don't want "getAnimals" to be the ID - I want it to be the action, instead.
How do I do that?
Assuming you also have a resources :clients entry, you want to make sure match '/clients/getAnimals', to: "clients#getAnimals" is above it (Rails will match whatever it hits first).
However, the better way may be to put it in the resource:
resources :clients do
get 'getAnimals', :on => :collection
end
Related
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.
I want to add another action to my controller, and I can't figure out how.
I found this on RailsCasts, and on most StackOverflow topics:
# routes.rb
resources :items, :collection => {:schedule => :post, :save_scheduling => :put}
# items_controller.rb
...
def schedule
end
def save_scheduling
end
# items index view:
<%= link_to 'Schedule', schedule_item_path(item) %>
But it gives me the error:
undefined method `schedule_item_path' for #<#<Class:0x6287b50>:0x62730c0>
Not sure where I should go from here.
A nicer way to write
resources :items, :collection => {:schedule => :post, :save_scheduling => :put}
is
resources :items do
collection do
post :schedule
put :save_scheduling
end
end
This is going to create URLs like
/items/schedule
/items/save_scheduling
Because you're passing an item into your schedule_... route method, you likely want member routes instead of collection routes.
resources :items do
member do
post :schedule
put :save_scheduling
end
end
This is going to create URLs like
/items/:id/schedule
/items/:id/save_scheduling
Now a route method schedule_item_path accepting an Item instance will be available. The final issue is, your link_to as it stands is going to generate a GET request, not a POST request as your route requires. You need to specify this as a :method option.
link_to("Title here", schedule_item_path(item), method: :post, ...)
Recommended Reading: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Ref Rails Routing from the Outside In
Following should work
resources :items do
collection do
post 'schedule'
put 'save_scheduling'
end
end
You can write routes.rb like this:
match "items/schedule" => "items#schedule", :via => :post, :as => :schedule_item
match "items/save_scheduling" => "items#save_scheduling", :via => :put, :as => :save_scheduling_item
And the link_to helper can not send post verb in Rails 3.
You can see the Rails Routing from the Outside In
I have this custom route in my routes.rb
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
resources :businesses
And I have constructed a link like this:
<%= link_to business.name, business_permalink_path %>
However, whenever I visit the page with that link, I get this error:
No route matches {:controller=>"businesses", :action=>"show"}
I tried inverting the route order:
resources :businesses
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
This does not work. It works if I change the link to this:
The show action exists and is defined in the file controllers/businesses_controller.rb.I want to create a custom URL using my permalink.
I am new in Rails and I know I am just missing something. What am I missing?
Try this:
<%= link_to business.name, business_permalink_path(business.permalink) %>
Try this:
match '/businesses/:permalink' => 'businesses#show', :as => :business_permalink
More here: http://railscasts.com/episodes/203-routing-in-rails-3
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} %>
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