I'm using devise for sign up and ActiveStorage for image upload. For the delete/purge function to work I have this route
devise_scope :user do
scope module: :users do
resources :registrations do
member do
delete :delete_image_attachment
end
end
end
end
But another place in my routes file I have this route
devise_for :users, controllers: {:registrations => "users/registrations"
}
It makes some of my pages not working. I have read somewhere that it's because registrations are declared two times. How can I make it work?
Any help would be much appreciated
If you use resources :registrations, only: [] do... that will create the parent route that you need without overwriting any of the routes provided by devise. Allowing you to make your nested routes :D
Related
i've seen a bunch of posts on how to rename already-declared routes in devise. I want to expand devise to have my own route check for and idle session. I am implementing a simple js check every 1 minute that I want to hit 'check_active' in the devise sessions controller. I tried this but no luck:
devise_scope :sessions do
get 'check_active'
end
Is there way to expand devise with a custom route (not rename an already-existing one) ?
UPDATE - almost there, i did this
# already had this in routes
devise_for :users, :controllers =>
{ registrations: 'registrations',
confirmations: 'confirmations',
sessions: 'sessions',
passwords: 'passwords',
omniauth_callbacks: "omniauth_callbacks"}
# added this
devise_scope :sessions do
get '/check_active' => 'sessions#check_active'
end
I have a js firing, i have it get '/check_active' as rake routes shows this:
check_active GET /check_active(.:format)
But when it fires, the controller 404s with
AbstractController::ActionNotFound (Could not find devise mapping for path "/check_active".
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]
):
If you are overwriting Devise's default controllers, it is not any different from any other controller to add your own route.
After you create your devise controllers to overwrite, do the following:
Under sessions_controller declare a method
# app/controllers/devise/sessions_controller.rb
def check_active
# do what you want to do
end
And in your router:
# config/routes.rb
devise_scope :sessions do
get 'check_active', to: "devise/sessions#check_active"
end
I was trying the same thing and realized that the scope should be for user and not sessions, also ensure that it has to be singular.
devise_scope :user do
get '/check_active' => 'sessions#check_active'
end
Edit: Adding link to help docs for better understanding
I currently have a default devise set up and wanting to put the user id's within my logged in routes and leave the others alone such as Welcome,FAQ, About us etc.
Such as
www.example.com/user-id/offers
My Current Routes File
devise_for :users
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :offers do
member do
put :tourcomplete
end
end
resources :categories, only: :show
root 'welcome#index'
get '/balance', to: 'balance#show', as: 'balance'
patch '/balance', to: 'balance#paypal', as: 'balance_paypal'
patch '/withdraw', to: 'balance#withdraw', as: 'balance_withdraw'
I can't find any docs on this and my previous answer to a similar question was very vague and not helpful.
Thanks
You will need to use a nested routes:
resources :users do
resources :offers do
member do
put :tourcomplete
end
end
end
so now your routes will be /users/:id/offers, run rake routes to check your routes.
if you want to exclude users then you will have to specify the route yourself:
match ':user_id/offers' => 'offers#index'
Pretty simple question, but I can't seem to find the answer with a good ole fashioned Google.
The error:
undefined method `user_signed_in?' for #<ActionDispatch::Routing::Mapper:0x007fc6369320e8> (NoMethodError)
Server won't even start.
My code:
Rails.application.routes.draw do
devise_for :users, :path_prefix => 'u'
resources :users
devise_scope :user do
get "login", to: "devise/sessions#new", as: :login
get 'logout', to: 'devise/sessions#destroy', as: :logout
get 'user/edit', to: 'devise/registrations#edit', as: :change_password
end
resources :apps do
resources :elements, controller: 'apps/elements'
end
resources :elements do
resources :features, except: [:index], controller: 'apps/elements/features'
end
if user_signed_in?
root to: 'apps#index'
else
devise_scope :user do
root to: 'devise/sessions#new'
end
end
end
What's the best way to go about making this work? I'm hesitant to try to work around it and make the site vulnerable, being a new RoR user. Thanks in advance.
In Rails access control is done on the controller layer - not on the routing layer.
Routes just define matchers for different sets of params and request urls. They are processed before rails even starts processing the request, they don't know anything about the session. Rails could have even used YML to define routes, except that Ruby is better at DSLs.
If you want to require that the user is signed in you would use:
class SomeController < ApplicationController
before_action :authenticate_user! # a Devise helper method
end
I have a requirement that I have to append abc/admin in each url. for e.g.
http://0.0.0.0:3000/abc/admin/admins/sign_in # USING ADMIN AS DEVISE MODEL
http://0.0.0.0:3000/abs/admin/products
http://0.0.0.0:3000/abs/admin/products/:id
http://0.0.0.0:3000/abs/admin/categories
etc. Now I tried with
scope "abc/admin" do
resources :products, :categories
end
It creates some URL eg:
products GET /abc/admin/products(.:format) products#index
POST /abc/admin/products(.:format) products#create
new_role GET /abc/admin/products/new(.:format) products#new
for Admin Model I tried using
scope "abc/admin" do
devise_for :admins, :controllers => {:sessions=>"admin/sessions", :registrations=>'admin/registrations'}
end
It also create URL like:
admin_registration POST /abc/admin/admins(.:format) admin/registrations#create
new_admin_registration GET /abc/admin/admins/sign_up(.:format) admin/registrations#new
Now new_admin_registration_path redirect to http://0.0.0.0:3000/admins/sign_up It is not considering the scope. I don't know how to achieve scope parameter in URL for devise as well as for other controller.
#Choco is right, you should only declare a namespace once. Then everything goes inside that single block.
namespace "abc/admin" do
resources :products, :categories
devise_for :admins, :controllers => {:sessions=>"admin/sessions", :registrations=>'admin/registrations'}
end
Then your path helper becomes new_abc_admin_admin_registration_path. See here for info: difference between scope and namespace of ruby-on-rails 3 routing
Whenever I click the sign in or sign up button on my site, I receive this error:
Routing Error -- No route matches [POST]"/sessions/user"
Here is my routes file:
Here are my routes:
Any ideas as to why I am receiving this error?
I think you want to post to user/sessions instead of sessions/user
Devise uses it's own methods to generate routes, is it possible that resources :sessions is overriding the methods devise needs to exist? I'm almost positive devise defines a method called new_session_path(resource_name, resource). Your resources :sessions may be stepping on Devise's toes so to speak.
EDIT
I have just confirmed this by cloning devise and running grep -R session_path .. You need to remove resources :sessions from your code, or scope it into a different namespace using :as.
resources :sessions, :as => 'sessions_that_dont_anger_devise'
#=>
new_sessions_that_dont_anger_devise_path
sessions_that_dont_anger_devise_path
... etc.
resources :users, only: [:show, :new, :create] do
member do
get :following, :followers
end
end
Fixed by removing the first resources :users and stuffing it into this method.