Could not find devise mapping for path - ruby

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.

Related

Rails Nameerror with double namespace controller, namespaced model

Can't work out how to get around this. I'm using the mailboxer gem, who have recently updated to a namespaced model. Using the main controller, everything is fine, but I also have a namespaced views/controller for an admin section which is causing the issue.
Model name is
mailboxer_conversations
Main section code (working fine):
routes.rb
namespace :mailboxer, path: '', as: nil do
resources :conversations, only: [:index, :show, :new, :create, :destroy], as: 'conversations', path: 'conversations' do
member do
post :reply
post :trash
post :untrash
end
end
controllers/mailboxer/conversations_controller.rb
class Mailboxer::ConversationsController < ApplicationController
end
The above is all working fine, the controller is namespaced with folders and I can access the Conversation model instance.
The below is the code I'm currently trying for the admin section, using the same model mailboxer_conversations.rb
routes.rb
namespace :admin do
namespace :mailboxer, path: '', as: nil do
resources :conversations, as: 'conversations', path: 'conversations'
end
end
controllers/admin/mailboxer/conversations_controller.rb
class Admin::Mailboxer::ConversationsController < ApplicationController
end
With the above setup, I'm getting a nameerror uninit. constant 'Conversations'. Says to me that it can't access the model, is this because of the double namespace, it's expecting the model.rb file to be in a different folder i.e admin/mailboxer_conversations.rb? I can't move the model, as it's in a gem.
Thanks

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"

Rails Routing Admin Error

I have this AdminController
class Admin::AdminController < ApplicationController
before_filter :is_admin?
def dashboard
end
def is_admin?
redirect_to root_path, :flash => { :alert => "You are not an admin!" } if !current_user.admin?
end
end
and this other controller that inherits from the above:
class Admin::CompetitionEntriesController < Admin::AdminController
before_action :set_competition_entry, only: [:show, :edit, :update, :destroy]
....
end
My route file is:
Foo::Application.routes.draw do
root 'competition_entries#index'
devise_for :users
resources :competition_entries
namespace :admin do
root 'admin#dashboard'
resources :competition_entries
end
....
..
.
end
Now why am I getting this error when I am trying to reach 'http://localhost:3000/admin'
Missing template admin/admin/dashboard...
I am getting this extra admin? Why? I don't want to use scopes I want to use namespaces.
Thanks.
Routes do not affect default search paths for templates. If your controller class is named Foo::BarController, Rails will look for the templates in app/views/foo/bar/ unless you specify otherwise.

Overriding Session Controller Prevents custom views being used

Devise (2.1) was using my custom views fine until I told it to use a custom controller. Now it ignores my custom views.
Previously everything worked fine:
Tell Devise to use custom views in /config/devise.rb
# ==> Scopes configuration
# Turn scoped views on. Before rendering "sessions/new", it will first check for
# "users/sessions/new". It's turned off by default because it's slower if you
# are using only default views.
config.scoped_views = true
Add custom view: /app/views/subscribers/session/new.html.erb
Set up routes in /config/routes.rb
devise_for :subscribers
Then I added a custom SubscriberSessionsController as /app/controllers/subscriber_session_controller.rb
class SubscriberSessionsController < Devise::SessionsController
before_filter :isInIframe
private
def isInIframe
#hide_navbar = session[:in_iframe]
end
end
And modified /config/routes.rb to tell Devise to use this new controller instead of its default:
devise_for :subscribers, :controllers => {
:sessions => "subscriber_sessions"
}
Once I restart my server, Devise now uses this controller but ignores my custom view.
As is so often the case, ten minutes after posting the question I cracked it.
The reason Devise wasn't finding the view was it was looking for it in a different folder.My replacement controller was called subscriber_sessions.rbso devise was no longer looking in views/subscribers/sessions but views/subscribers/subscriber_sessions.
I solved this problem with the following:
Changed my subscriber routes to:
devise_for :subscribers, :controllers => {
:sessions => "subscribers/sessions"
}
Renamed my subscriber_sessions controller to just sessions and moved it into a subscribers folder so its new name & location are: app/controllers/subscribers/sessions_controller.rb
I also had to add a namespace to the class so the new sessions_controller.rb file looks like this"
class Subscribers::SessionsController < Devise::SessionsController
before_filter :isInIframe
private
def isInIframe
#hide_navbar = session[:in_iframe]
end
end

Resources