RSpec controller lookup - ruby

I'm trying to define a RSpec test for a controller named FindWithSameDirectorController.
I got a ActionController::RoutingError. No route matches {:id => 1, :controller => 'find_with_same_director', : action => 'show'}.
The controller name doesn't match the controller that i define.
How does rspec determines this name ? Is it possible to change it ?
Thx
Bertrand

Your spec propably looks like this:
describe FindWithSameDirectorController do
it { should route(:get, '/find_with_same_director/1').to action: 'show', id: 1 }
end
but in your config/routes.rb you have either missing a line like
resources :find_with_same_director
(or this resource is overwritten by another route specification).

Related

Unexpected controller and action values

I am viewing my parameters when testing my ruby on rails application, and the controller and action values are not what I would expect based on the routes.rb file or when running "rake routes".
For example, the URL http://localhost:3000/crimpers/1
yields the parameters of:
{ controller: ecrimp, action: show, id: 1}
whereas I would expect it to be:
{ controller: crimpers, action: show, id:1 }
However, for http://localhost:3000/crimpers, the parameters are the expected:
{ controller: crimpers, action: index }
crimpers_controller.rb contains the actions index, create, update, edit, show, new and destroy.
ecrimp_controller.rb contains the actions welcome and ecrimp.
routes.rb:
root 'ecrimp#welcome'
get 'ecrimp' => 'ecrimp#ecrimp'
resources :crimpers
resources :hose_specs
resources :hose_specs do
collection { post :import }
Why might the controller and action be defined in such a way for the first url?

How to write specific custom routes for rails + devise?

I am trying to write a custom route that will point to a custom controller action in devise.
I have the setup below right now.
# custom controller
class Registrations::RegistrationsController < Devise::RegistrationsController
layout 'settings'
# GET /resource/edit
def edit
super
end
end
# routing setup
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "registrations/registrations" },
path_names: { edit: 'profile' }
end
This allows me to have a custom URL localhost:4000/users/profile with no problems.
My question is how can I customize this further to be
localhost:4000/profile
localhost:4000/settings/profile
Note I know that I can set path: '' or path: 'settings', but that will affect all routes within users.
Is there a way that I could have
localhost:4000/settings/profile and localhost:4000/login at the same time using devise_for?
I am not sure how to control these affects separately.
As we can see here, we can use Rails scopes and specify a controller for 'registration', for example. Something like this:
scope :settings do
devise_scope :user do
get '/profile' => 'devise/registrations#edit'
end
end

Create a link to a nested resource where the parent does not have resource name in the URL

I have setup my routes to work without the resource in the url (i.e. /username/posts, as opposed to /users/username/posts) - using friendly_id gem inorder to use :username instead of :id.
The resources are under a namespace (cpanel) and work fine.
My routes are setup like this:
namespace :cpanel do
resources :users, :path => '', :constraints => { :id => /[\w+\-\_]+/ } do
resources :posts
end
end
Navigating to /cpanel/username/posts works fine, but I am having trouble setting up my link_to I am using the path cpanel_user_posts_path(#user) but this creates a link to the URL /cpanel/users/:username/apps.
How can I create a link to the path: /cpanel/:username/apps?
Thanks in advance.
Did you try something like this:
get 'cpanel/:username/apps', to: 'controller#action
You can always hard code it (as a last resort) via:
link_to "Apps", "/#{current_user.username}/apps"

easy way to rename URL in Rails?

I just want to make a change to the URL without getting an undefined method error. I can try to do it manually but is there an easy way to do so?
For example, on my local server, if the URL is:
/users/username/XYZ
Is there an easy way to change "XYZ" to something else easily?
If XYZ is a part of a resourceful route you may modify it like this
resources :users, :path_names => { :new => 'make', :edit => 'change' }
OR
if you want to change the name of the resource only you may change it like this:
resources :person, :controller => 'users', :as => 'users'
this would change the urls to 'person' and maintaining all class and method names.
Here is the rake routes output:
users GET /person(.:format) users#index
POST /person(.:format) users#create
new_user GET /person/new(.:format) users#new
edit_user GET /person/:id/edit(.:format) users#edit
user GET /person/:id(.:format) users#show
PUT /person/:id(.:format) users#update
DELETE /person/:id(.:format) users#destroy
You can find out more on how to customize resourceful routes here:
http://guides.rubyonrails.org/routing.html#customizing-resourceful-routes
I think this will be exactly what u are looking for :) ...
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
http://railscasts.com/episodes/63-model-name-in-url-revised
Regards

Undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1 displayed in Rspec Controllers

After running the following code from Rspec - Controllers, I get an error from the get method
it "assigns #MyItems" do
my_item = mock(:mypay_items)
my_item = mock( MyItem)
MyItem.should_receive(:all).and_return(my)
get 'index'
assigns[:my_items].should eql(my_items)
response.should be_success
end
It results in an error:
undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x34b6ae0>
It would seem that you're not properly declaring your spec as a controller spec, which results in the HTTP request methods (get, post, etc.) not being available. Make sure that at the top of your spec, you have something like:
describe PostsController do
...
end
Replace PostsController with the name of your controller. If that doesn't work, add :type => :controller:
describe PostsController, :type => :controller do
...
end
See also this answer: undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8>
If at all you are using 'spec/features', you may need to add the following to your 'spec_helper.rb'
config.include RSpec::Rails::RequestExampleGroup, type: :feature
I had the same problem and the solution that worked for me was to add require 'rspec/rails' to my spec_helper file. All my controllers were setup correctly and adding the :type => controller didn't help.

Resources