How to write specific custom routes for rails + devise? - ruby

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

Related

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.

Shopify Webhook not sending to controller

Thru the Shopify API I can create webhooks great. I've created ones that point to RequestBin just fine, the data that is sent to RequestBin is perfect. But when I turn it towards my own controllers nothing happens. I have a binding.pry in my controller to catch anything that comes in, and nothing ever comes in. Not sure why??
Here is me creating the hook with the ShopifyAPI gem:
webhook = ShopifyAPI::Webhook.create(:topic => "carts/update",
:format => "json",:address => "https://dyno-shipping-trimakas.c9users.io
/webhook/cart_callback/")
And then here it is verified when I list the webhooks:
{"id"=>226014599,
"address"=>
"https://dyno-shipping-trimakas.c9users.io/webhook/cart_callback/",
"topic"=>"carts/create",
"created_at"=>"2016-02-24T17:20:02-05:00",
"updated_at"=>"2016-02-24T17:20:02-05:00",
"format"=>"json",
"fields"=>[],
"metafield_namespaces"=>[]}
This is my controller:
class WebHookController < ApplicationController
skip_before_action :verify_authenticity_token, only: [:cart_callback]
def cart_callback
binding.pry
x = params
pp x
end
end
Then finally this is my route:
scope '/webhook', :controller => :webhook do
post :cart_callback
end
Not sure where I went astray??

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

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"

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