I have this spec:
specify { expect(:post => admin_featured_path()).to route_to(:controller => 'admin/featured', :action => 'create')}
I cant' make it pass however it seems logical that the post to the route should be routed to the create action...
This is my route file:
namespace :admin do
resources :featured, only: [:index, :update, :destroy, :create]
end
This is the failure message:
1) Featured routes
Failure/Error: specify { expect(:post => admin_featured_path()).to route_to(:controller => 'admin/featured', :action => 'create')}
ActionController::UrlGenerationError:
No route matches {:action=>"update", :controller=>"admin/featured"} missing required keys: [:id]
# ./spec/routing/featured_spec.rb:7:in `block (2 levels) in <top (required)>'
This should work (untested):
page = post admin_featured_path
expect(page).to route_to(:controller => 'admin/featured', :action => 'create')
{:post => admin_featured_path}.should route_to(:controller => 'admin/featured', :action => 'create')
or
{:post => "admin/featured"}.should route_to(:controller => "admin/featured", :action => 'create')
Related
I am trying to use url_for in mailer view for a defined route:
<%= url_for(
:controller => 'scribe_requests',
:action => 'accept',
:id => #match.acceptance_token,
:only_path => false) %>
I have defined the route in my routes.rb:
resources :scribe_requests do
member do
match 'accept' => 'scribe_requests#accept', :as => :accept
end
end
And my controller:
class ScribeRequestsController < ApplicationController
respond_to :html
def accept
..
end
..
end
I am not sure what is going wrong here? My delayed job is failing with the exception
"{No route matches {:controller=>"scribe_requests", :action=>"accept",
:id=>"nv4Nl8wWXLX2zFDm3s3t7w"}
/home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.2.5/lib/action_dispatch/routing/route_set.rb:532:in
raise_routing_error'
/home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.2.5/lib/action_dispatch/routing/route_set.rb:528:in
rescue in generate'
/home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.2.5/lib/action_dispatch/routing/route_set.rb:520:in
generate'
/home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.2.5/lib/action_dispatch/routing/route_set.rb:561:in
generate'
/home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.2.5/lib/action_dispatch/routing/route_set.rb:586:in
url_for'
/home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.2.5/lib/action_dispatch/routing/url_for.rb:148:in
url_for'
/home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.2.5/lib/action_view/helpers/url_helper.rb:107:in url_for'
/home/syed/work/projects/mapunity/retina-india/app/views/notifier/scribe_service_needed_email.html.erb:47:in
_app_views_notifier_scribe_service_needed_email_html_erb___981003510_106966530'
/home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.2.5/lib/action_view/template.rb:145:in
`block in render'
Had to add :via option to the route definition and it worked. Weirdly, I had done that with no affect. May be some caching issue. So after the change, my route definition became,
resources :scribe_requests do
member do
match 'accept' => 'scribe_requests#accept', :via => :get, :as => :accept
end
end
Test run:
ruby-1.9.2-p0 > app.url_for :controller => 'scribe_requests', :action => 'accept', :id => 'test'
=> "http://www.example.com/scribe_requests/test/accept"
Thanks for the help.
Why do I have:
Unknown action
No action responded to 12345-yyyymmdd-6789-ABC-Another_Test. Actions: index, show, and sub_layout
url
localhost:3000/asset/recordings/12345-yyyymmdd-6789-ABC-Another_Test?time=00%3A25%3A05
Here is route.rb:
map.namespace :asset do |asset|
map.resources :asset, :controller => 'asset/recordings', :action => 'index' # index of asset
asset.resources :recordings, :controller => 'recordings'
asset.resources :recordings do |recording|
recording.resource :transcript
end
asset.resources :clip_lists, :controller => 'clip_lists'
map.delete_asset_clip_list 'asset/clip_list/:id/destroy', :controller => 'asset/clip_lists', :action => 'destroy'
map.asset_clip_list 'asset/clip_list/:id', :controller => 'asset/clip_lists', :action => 'show'
asset.connect ':asset/controller/:action/:id'
asset.connect ':asset/controller/:action/:id.:format'
end
If I add manually show?id= in the URL it works:
localhost:3000/asset/recordings/show?id=12345-yyyymmdd-6789-ABC-Another_Test?time=00%3A25%3A05
I am trying to make a test for a controller for a nested resource.
The nesting is like this in the routes.rb
resources :cars, :only => [:index, :destroy, :show] do
resources :car_subscriptions, :only => [:new, :create], :as => :follow_subscriptions
end
I'm trying to test the create action most specifically:
describe CarSubscriptionsController do
def valid_attributes
{:car_id => '1', :user_id => '2'}
end
describe "POST create" do
describe "with valid params" do
it "creates a new CarSubscription" do
expect {
post :create, :car_id => 1, :car_subscription => valid_attributes
}.to change(CarSubscription, :count).by(1)
end
it "assigns a newly created car_subscription as #car_subscription" do
post :create, :car_subscription => valid_attributes
assigns(:car_subscription).should be_a(CarSubscription)
assigns(:car_subscription).should be_persisted
end
it "redirects to the created car_subscription" do
post :create, :car_subscription => valid_attributes
response.should redirect_to(CarSubscription.last)
end
end
end
end
It's actually a part of the scaffold generated by rails script. And I only modified the valid_attributes and the post in the first 'it'
And the output is this:
1) CarSubscriptionsController POST create with valid params creates a new CarSubscription
Failure/Error: post :create, :car_id => 1, :car_subscription => valid_attributes
ActionController::RoutingError:
No route matches {:car_id=>"1", :car_subscription=>{:car_id=>"1", :user_id=>"2"}, :controller=>"car_subscriptions", :action=>"create"}
# ./spec/controllers/car_subscriptions_controller_spec.rb:34:in `block (5 levels) in <top (required)>'
# ./spec/controllers/car_subscriptions_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
It's the same error for all 'it's.
I've tried removing the :as => :following_subscriptions from the routes.rb file but the same problem.
I have actually split up the resources of car_subscriptions so index and destroy are in not nested, and create and new are nested in :cars
I don't want to use hard coded paths like in this answer but if it is the only way, I can give it a try:
{ :post => "/forum_topics/1/forum_sub_topics" }.should route_to(:controller => "forum_sub_topics", :action => "create", :forum_topic_id => 1)
EDIT
Oh, and my rake routes looks like this:
car_follow_subscriptions_da POST /biler/:car_id/car_subscriptions(.:format) {:action=>"create", :controller=>"car_subscriptions", :locale=>"da"}
From what rake routes provides, I guess you should replace:
post :create, :car_id => 1, :car_subscription => valid_attributes
with:
post :create, :car_id => 1, :car_subscription => valid_attributes, :locale => "da"
How can I link up my routes so that the spec will pass? I have read here http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
The spec
require 'spec_helper'
describe ConversationMembersController do
describe "routing" do
it "recognizes and generates #index" do
{ :get => "/conversations/123/members" }.should route_to(:controller => "conversation_members", :action => "index", :conversation_id=>"123")
end
end
end
Failing spec
ConversationMembersController routing recognizes and generates #index
Failure/Error: { :get => "/conversations/123/members" }.should route_to(:controller => "conversation_members", :action => "index", :conversation_id=>"123")
The recognized options <{"action"=>"123", "id"=>"members", "controller"=>"conversations"}> did not match <{"conversation_id"=>"123",
"action"=>"index",
"controller"=>"conversation_members"}>, difference: <{"conversation_id"=>"123",
"action"=>"index",
"id"=>"members",
"controller"=>"conversation_members"}>
The routes
resources :conversations, :except => [:edit] do
resources :conversation_members, :as => "members", :except => [:show, :edit, :update, :destroy] do
collection do
delete :leave
end
end
The output of rake routes | grep conversation_members
52: leave_conversation_members DELETE /conversations/:conversation_id/conversation_members/leave(.:format) {:action=>"leave", :controller=>"conversation_members"}
53: conversation_members GET /conversations/:conversation_id/conversation_members(.:format) {:action=>"index", :controller=>"conversation_members"}
54: POST /conversations/:conversation_id/conversation_members(.:format) {:action=>"create", :controller=>"conversation_members"}
55: new_conversation_member GET /conversations/:conversation_id/conversation_members/new(.:format) {:action=>"new", :controller=>"conversation_members"}
try this?
resources :conversations, :except => [:edit] do
resources :members, :controller => "conversation_members", :except => [:show, :edit, :update, :destroy] do
collection do
delete :leave
end
end
end
From the documentation it appears that the :as option just changes the named helpers...so the actual url is still
/conversations/xxx/conversation_members
but you can refer to the route as
conversation_members_path
check out this great guide to routing
We've got a rails app that keeps producing an error that we can't track down. The problem seems to lie with the routing but we're not sure what is happening. Out routes file looks like:
ActionController::Routing::Routes.draw do |map|
# Default
map.home '', :controller => "home"
# Admin
map.admin_home 'admin', :controller => 'admin/admin', :action => 'index'
map.admin_login 'admin/login', :controller => 'admin/admin', :action => 'login'
map.admin_reminder 'admin/forgot', :controller => 'admin/admin', :action => 'reminder'
map.namespace :admin do |admin|
admin.resources :bookings,
:collection => {
:archive => :get, :reports => :get, :calendar => :get,
:step_1 => :any, :step_1a => :any, :step_2 => :any, :step_3 => :any, :confirmation => :any }
admin.resources :events,
:member => { :status => :get }
admin.resources :blogs,
:collection => { :archive => :get }
admin.resources :blog_replies,
:member => { :publish => :get }
admin.resources :minutes,
:collection => { :archive => :get }
admin.resources :businesses
admin.resources :business_categories
admin.resources :users
admin.resources :pricings
admin.backups 'backups', :controller => 'admin/backups'
admin.download_backup 'backups/download', :controller => 'admin/backups', :action => 'download'
end
map.admin 'admin/:action', :controller => 'admin/admin'
map.connect 'members', :controller => 'admin/admin', :action => 'redirect_to_index'
map.connect 'members/login', :controller => 'admin/admin', :action => 'redirect_to_index'
map.connect 'account', :controller => 'admin/admin', :action => 'redirect_to_index'
map.connect 'account/login', :controller => 'admin/admin', :action => 'redirect_to_index'
map.connect 'home', :controller => 'admin/admin', :action => 'redirect_to_index'
map.connect 'home/login', :controller => 'admin/admin', :action => 'redirect_to_index'
map.blog 'blog/:permalink', :controller => 'blogs', :action => 'show'
map.connect 'blog/:id', :controller => 'blogs', :action => 'show'
map.connect 'book-online', :controller => 'bookings', :action => 'step_1'
map.connect 'book-online/:action', :controller => 'bookings'
# Defaults
map.connect ':controller/:action/:id'
map.connect "*anything", :controller => "public", :action => "unknown_request"
end
We have a set of public controllers in app/controllers and a set of admin controllers in app/controllers/admin. The issue we keep seeing is when a user goes to a url like admin/bookings, admin/bookings/step_1 or admin/events. Sometimes the urls work perfectly but other times I can see from the log file that something like the following happens:
ActionController::UnknownAction (No action responded to index):
Other times we'll get something like:
Processing EventsController#index (for [filtered] at 2009-01-21 10:54:38) [GET]
Session ID: [filtered]
Parameters: {"action"=>"index", "controller"=>"admin/events"}
Rendering template within layouts/public
Rendering events/index
Completed in 0.00863 (115 reqs/sec) | Rendering: 0.00338 (39%) | DB: 0.00000 (0%) | 200 OK [http://www.gresfordtrust.org/admin/events]
from the last example you can see that the url requested was admin/events which should have hit the #index in Admin::EventsController but instead it renders the #index action in EventsController.
We're running the app with Rails 2.0.2.
You don't have a configured route for EventsController, so your error is happening because some request is falling down to the default route, map.connect ':controller/:action/:id'
.
This is happening because someone/something is sending a request of an HTTP method that isn't configured for your AdminEventsController. Your admin.resources :events, :member => { :status => :get } will match the following requests:
GET /admin/events
GET /admin/events/<id>
GET /admin/events/<id>/status
POST /admin/events
PUT /admin/events/<id>
DELETE /admin/events/<id>
Anything else would fall through to the default route. So if you're seeing those ActionController::UnknownAction on this controller look for requests that are using the wrong HTTP method.
The source of your bizarre log message is almost certainly a request that was something like this:
GET /admin/events/index
The solution is to get rid of that default route entirely and ensure you're adding resource[s] routes for all the controllers in the right place.