easy way to rename URL in Rails? - ruby

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

Related

Change default URL param name when using Route::resource

When using Route::resource(), Laravel of course 'chooses' somethings for you, such as route names and methods.
I know how to override, for example, the allowed routes/methods:
Route::resource('user', 'UserController', array('only' => array('index', 'show')));
But I now need to override the URL param name that Laravel sets for the user routes. By default, in the example above, it will be user. But, I want it to be user_id.
Does Laravel provide a way to set this, when using Route::resource?
So that, for example, I would end up with the route:
mydomain.com/users/{user_id}
rather than:
mydomain.com/users/{user}
Thanks
Found it. Yes, Laravel does provide a way to override this when using Route::resource().
Route::resource('users', 'UserController')->parameters([
'users' => 'user_id'
]);
The key of the element in the array in the argument to 'parameters' is the same as what you enter as the first argument of the 'resource' method (not the 'singular' version or something)

Could not find devise mapping for path

I have the following routes defined in my routes.rb
devise_for :users, controllers: { registrations: "network/registrations",sessions: 'network/sessions', passwords: 'network/passwords' }
devise_scope :user do
get "registrations/show" => "network/registrations", as: :show_user_profile
end
and when i do rake routes i also see
network_show_user_profile GET /network/registrations/show(.:format) network/registrations#show
But when i try to access the path /network/registrations/show
i get the below exception
Could not find devise mapping for path "/network/registrations/show".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
get "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
#request.env["devise.mapping"] = Devise.mappings[:user]
I tried to modify the routes.rb and added
devise_scope :user do
get 'user_profile' => 'registrations#search', :as => 'user_profile'
end
and when i access user_profile path, i get the error
The action 'show' could not be found for Network::RegistrationsController
but when i add the action show to the controller I again get the same exception message
Could not find devise mapping for path "/network/registrations/show".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
get "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
#request.env["devise.mapping"] = Devise.mappings[:user]
any help as to what i am doing wrong would be appreciated. Thanks.
I tried adding same routes that you did
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "network/registrations",sessions: 'network/sessions', passwords: 'network/passwords' }
devise_scope :user do
get "registrations/show" => "network/registrations", as: :show_user_profile
end
end
But instead of
network_show_user_profile GET /network/registrations/show(.:format)
I got show_user_profile GET /registrations/show(.:format) registrations#show
So I replaced this routes file with below
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "network/registrations",sessions: 'network/sessions', passwords: 'network/passwords' }
devise_scope :user do
get "/network/registrations/show" => "network/registrations#show", as: :show_user_profile
end
end
I also created an action show in network/registrations_controller.rb and
everything works perfectly for me.
Hope this helps.

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

RSpec controller lookup

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).

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"

Resources