No route matches although there is one in rake routes - ruby-on-rails

I get strange ActionController::RoutingError: No route matches. The route can be generated:
> r = Rails.application.routes
> r.generate controller: :items, action: :index, user_id:1
=> ["/users/1/items", {}]
And it is in the rake routes:
$ rake routes
user_items GET /users/:user_id/items(.:format) items#index
POST /users/:user_id/items(.:format) items#create
new_user_item GET /users/:user_id/items/new(.:format) items#new
edit_user_item GET /users/:user_id/items/:id/edit(.:format) items#edit
user_item GET /users/:user_id/items/:id(.:format) items#show
PUT /users/:user_id/items/:id(.:format) items#update
DELETE /users/:user_id/items/:id(.:format) items#destroy
(...)
But I it's not recognized:
r.recognize_path "/users/1/items"
ActionController::RoutingError: No route matches "/users/1/items"
My routes.rb:
Sumo2::Application.routes.draw do
resources :users do
resources :items
end
(...)
end
Any ideas?

Do you have ItemsController class defined? Without it your route won't be recognized. Perhaps you misspelled it.
irb(main):001:0> r = Rails.application.routes
=> #<ActionDispatch::Routing::RouteSet:0x1cbba00>
irb(main):002:0> r.recognize_path "/users/1/items"
ActionController::RoutingError: No route matches "/users/1/items"
... errors ...
irb(main):003:0> class ItemsController; end # <----------<
=> nil
irb(main):004:0> r.recognize_path "/users/1/items"
=> {:action=>"index", :controller=>"items", :user_id=>"1"}

Related

rails 3 route path building issue

I have such route:
scope ':property', :constraints => { :property => /inv|um|tm|pp/ } do
resources :user_templates do
member do
get 'download/:idcreated', :action => 'download'
end
end
end
In browser http://127.0.0.1:3000/inv/user_templates/6/download/12?locale=uk, it works.
But How can I build resource path?
I tried:
download_user_template_path(params[:property], #id, #idcreated)
but I got that message:
No route matches {:action=>"download", :controller=>"user_templates", :locale=>:uk, :property=>"inv", :id=>76, :format=>6}
It doesn't work too:
download_user_template_path('property' => params[:property], 'id' => #tmp_id, 'idcreated' => #created_temp_row[:id])
rake routes
GET /:property/user_templates/:id/download/:idcreated(.:format) {:property=>/inv|um|tm|pp/, :action=>"download", :controller=>"user_templates"}
user_templates GET /:property/user_templates(.:format){:property=>/inv|um|tm|pp/, :action=>"index", :controller=>"user_templates"}
POST /:property/user_templates(.:format){:property=>/inv|um|tm|pp/, :action=>"create", :controller=>"user_templates"}
new_user_template GET /:property/user_templates/new(.:format){:property=>/inv|um|tm|pp/, :action=>"new", :controller=>"user_templates"}
edit_user_template GET /:property/user_templates/:id/edit(.:format){:property=>/inv|um|tm|pp/, :action=>"edit", :controller=>"user_templates"}
user_template GET /:property/user_templates/:id(.:format){:property=>/inv|um|tm|pp/, :action=>"show", :controller=>"user_templates"}
PUT /:property/user_templates/:id(.:format){:property=>/inv|um|tm|pp/, :action=>"update", :controller=>"user_templates"}
DELETE /:property/user_templates/:id(.:format){:property=>/inv|um|tm|pp/, :action=>"destroy", :controller=>"user_templates"}
Any help plz?
Simply add :as => :download, and use rake routes CONTROLLER= ...

route index for a resource

What is the equivalent index path for resources :topics if I were to write it out manually?
When I run rake routes with the resources line in the routes.rb file it shows.
GET /topics(.:format) {:action=>"index", :controller=>"topics"}
I have tried a few things with no success, for example:
get 'topics/index' #=> 'topics#index'
as the route says:
get "/topics" => "topics#index", :as => :topics
you can now use topics_path or topics_url

RSpec RoutingError for nested controller

I'm testing a nested controller and get the following error:
1) Checklists::ItemsController index action should render index template
Failure/Error: get :index, :checklist_id => checklist.id
ActionController::RoutingError:
No route matches {:checklist_id=>1, :controller=>"checklists/items"}
In the browser loading /checklists/1/items loads fine.
Am I missing something in the spec?
The routes:
resources :checklists do
resources :items, :controller => "Checklists::Items"
end
The controller located in namespaced folder (/app/controllers/checklists/items_controller.rb):
class Checklists::ItemsController < ApplicationController
respond_to :html, :json
def index
#checklist_items = #checklist.items
respond_with #checklist_items
end
end
The spec (/spec/controllers/checklists/items_controller_spec.rb):
describe Checklists::ItemsController do
let(:user) { Factory :user, :role => 'admin' }
let(:checklist) { Factory(:checklist) }
let(:checklist_item) { Factory(:checklist_item) }
before(:each) do
sign_in_to(controller, user)
Checklist.stub(:find => checklist)
end
it "index action should render index template" do
get :index, :checklist_id => checklist.id
response.should render_template(:index)
end
end
Update: Routes for checklist items
checklist_items GET /checklists/:checklist_id/items(.:format) {:action=>"index", :controller=>"Checklists::Items"}
POST /checklists/:checklist_id/items(.:format) {:action=>"create", :controller=>"Checklists::Items"}
new_checklist_item GET /checklists/:checklist_id/items/new(.:format) {:action=>"new", :controller=>"Checklists::Items"}
edit_checklist_item GET /checklists/:checklist_id/items/:id/edit(.:format) {:action=>"edit", :controller=>"Checklists::Items"}
checklist_item GET /checklists/:checklist_id/items/:id(.:format) {:action=>"show", :controller=>"Checklists::Items"}
PUT /checklists/:checklist_id/items/:id(.:format) {:action=>"update", :controller=>"Checklists::Items"}
DELETE /checklists/:checklist_id/items/:id(.:format) {:action=>"destroy", :controller=>"Checklists::Items"}
It turns out the solution to the problem was in the routes:
I changed
resources :items, :controller => "Checklists::Items"
to
resources :items, :controller => "checklists/items"
and it works now

Rails 3.1 has_one nested resource: routing not generating "all" paths

I have a has_one relation:
# supplier.rb
has_one :presentation
...
# presentation.rb
belongs_to :supplier
...
and the folowing nested routes for them:
# routes.rb
resources :suppliers do
resource :presentation
end
running rake routesgives:
supplier_presentation POST ... {:action=>"create", :controller=>"presentations"}
new_supplier_presentation GET ... {:action=>"new", :controller=>"presentations"}
edit_supplier_presentation GET ... {:action=>"edit", :controller=>"presentations"}
GET ... {:action=>"show", :controller=>"presentations"}
PUT ... {:action=>"update", :controller=>"presentations"}
DELETE ... {:action=>"destroy", :controller=>"presentations"}
Why no name_helper for the show action?
I can override the problem doing something like:
resources :suppliers do
resource :presentation, :except => :show do
get "" => "presentations#show", as: "presentation"
end
end
giving the route:
presentation_supplier_presentation GET ... {:controller=>"presentations", :action=>"show"}
but we all now that's not the right way to deal with it..
ANY SUGGESTIONS?
--
(edited)
supplier_presentation_path(#supplier)
does work, but why?... It doesn't appear when rake routes is executed on my shell...
I dont really know why it's not displayed when you do rake routes but did you try in your code to do supplier_presentation_path(#supplier)? It should work based on your routes.
Never the less it should work for you. Try this:
link_to "Presentation", [#suplier, #presentation]
or
link_to "Presentation", suplier_presentation_path(#suplier, #presentation)

Edit multiple records, controller & routing

I am following railscast 198 http://railscasts.com/episodes/198-edit-multiple-individually, trying to update it to rails 3, and am stuck with a routing error. The route http://localhost:3000/orders/edit_individual gives me the error:
ActiveRecord::RecordNotFound in OrdersController#show - Couldn't find Order with ID=edit_individual
I have updated and used his rails 2 routing
map.resources :products, :collection => { :edit_individual => :post, :update_individual => :put }
to the rails 3 convention as described by engineyard http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ (updated to my needs)
resources :orders do
collection do
post :edit_individual
put :update_individual
end
end
Here's what I've tried:
I've tried changing the routes resources as suggested by the answer to: Adding an action to an existing controller (Ruby on Rails) but the same error still shows. I've tried removing the cancan load and authorize resources, the 'resources :orders' route entry, as well as the 'show' entry in the controller, but another error indicates it's still trying to show a record with ID='edit_individual' instead of treating "edit_individual" like a route.
Here are my routes and controller,
myapp::Application.routes.draw do
resources :products
resources :roles
devise_for :users #, :controllers => { :registrations => "registrations" }
# match 'dashboard' => 'user_dashboard#index', :as => 'user_root'
resources :orders do
collection do
post :edit_individual
put :update_individual
end
end
resources :orders
class OrdersController < ApplicationController
load_and_authorize_resource # cancan method
def index
end
def show
end
def edit_individual #from railscast 198
#orders = current_user.customer_orders
end
def update_individual
#orders = Order.update(params[:orders].keys, params[:orders].values).reject { |p| p.errors.empty? }
if #orders.empty?
flash[:notice] = "Orders updated"
redirect_to orders_url
else
render :action => "edit_individual"
end
end # ...etc.
I've removed the cancan methods, but is it the culprit still? I've tried everything I can think of and am at a dead end.. any ideas?
edit:
the output from command prompt:
Started GET "/orders/edit_individual" for 127.0.0.1 at Thu Jun 30 11:19:02 -0700
2011
Processing by OrdersController#show as HTML
Parameters: {"id"=>"edit_individual"}
←[1m←[36mOrder Load (0.0ms)←[0m ←[1mSELECT "orders".* FROM "orders" WHERE "or
ders"."id" = 0 LIMIT 1←[0m
Completed in 2584ms
ActiveRecord::RecordNotFound (Couldn't find Order with ID=edit_individual):
and the route in my html:
<%= link_to 'Update Payments Received', edit_individual_orders_path %>
and rake routes:
edit_individual_orders POST /orders/edit_individual(.:format) {:action=>"edit_individual", :controller=>"orders"}
update_individual_orders PUT /orders/update_individual(.:format) {:action=>"update_individual", :controller=>"orders"}
orders GET /orders(.:format) {:action=>"index", :controller=>"orders"}
POST /orders(.:format) {:action=>"create", :controller=>"orders"}
new_order GET /orders/new(.:format) {:action=>"new", :controller=>"orders"}
edit_order GET /orders/:id/edit(.:format) {:action=>"edit", :controller=>"orders"}
order GET /orders/:id(.:format) {:action=>"show", :controller=>"orders"}
PUT /orders/:id(.:format) {:action=>"update", :controller=>"orders"}
DELETE /orders/:id(.:format) {:action=>"destroy", :controller=>"orders"}
You should probably run "rake routes" and see what route it gives for "edit_individual" action.
Also your log
Started GET "/orders/edit_individual" for 127.0.0.1 at Thu Jun 30 11:19:02 -0700
says that you are calling a post action as get.
Try below
resources :orders do
collection do
get :edit_individual
put :update_individual
end
end
OR either way you can use
<%= link_to 'Update Payments Received', edit_individual_orders_path, :method => "post" %>
So the error message: OrdersController#show - Couldn't find Order with ID=edit_individual tells me that for whatever reason you're being routed to the 'show' action, which is inherently expecting something like
/orders/1
meaning it's a member not a collection. Are you certain that the url being hit in your server output matches /orders/edit_individual?

Resources