I have a pretty straight forward app with a couple tweaks to Devise.
First I created a Registrations controller that class RegistrationsController < Devise::RegistrationsController inherits from Devise. I created this controller so that I could edit users without re-supplying passwords. https://gist.github.com/1514687
I also did this in my routes:
devise_for :users, :controllers => { :registrations => "registrations" }`
The signup works fine but when I call the following:
<p class="edit"><%= link_to "Edit", edit_user_registration_path(user) %></p>
The url it spits out is (running on localhost): http://localhost:3000/users/edit.2
Any ideas here?
I recommend a non-devise controller for doing this, and name it something other that "users" for the sake of not overlapping with devise routes
some key nomenclature:
rails g controller accounts
resources :accounts
def edit
#user = User.find(params[:id]
end
(other controller actions similar, just refer to #user and don't worry about that this happens to be called the accounts controller)
In your routes.rb file make sure that you have
resources :users
after your devise_for line like below:
devise_for :users, :controllers => { :registrations => "registrations" }
resources :users
Related
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
I'm trying to customize the after sign up path in my Devise registrations. Per the instructions I created a new controller called therapists_registrations_controller.rb, but when I start the server and load a page I get ActionController::RoutingError (uninitialized constant TherapistsRegistrationsController).
Now therapists_registrations_controller.rb is in app/controllers/api/therapists_registrations_controller.rb, all the other controllers load and I'm not playing any games with autoload_paths or anything. What could be the problem?
This is on Rails 3.2.11. Code for the controller is:
class TherapistsRegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
"http://google.com"
end
end
One of the reason could be the class name. As you have therapists_registrations_controller.rb inside the app/controllers/api, the class name should be
class Api::TherapistsRegistrationsController < Devise::RegistrationsController
Update:
You should also change this
devise_for :therapists, :controllers => { :registrations => "therapists_registrations" }
to
devise_for :therapists, :controllers => { :registrations => "api/therapists_registrations" }
i had three ror devise forms...so i thought of adding after_sign_up_path_for .its working perfectly but the problem i am having is one of the forms using session hascode..when its redirected it shows hashcode is not set...
i tried lot of thins but its working when i add devise_for :users alone in routes.rb the session code is set but if i add this line ,session is not set but all three forms are redirecting correctly..
devise_for :users, :controllers => {:registrations => "registrations"}
i am not sure what is causing the problem..please anybody help...
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
if resource.planner_type == "planner"
'/path1/invited_new'
elsif resource.planner_type == "bride"
'/path2/invited_new'
else
'/path3/invited_new'
end
end
end
I have the devise SessionController overwrited:
on app/controllers/customers/sessions_controller.rb
class Customers::SessionsController < Devise::SessionsController
before_filter :destroy_cart, only: :destroy
def destroy_cart
cart = Cart.find(current_client.cart.id)
cart.destroy
end
end
but the cart is never destroyed, even if I overwrite the destroy method directly and add the super after my code, the cart its still there, in the database (I knkow I could create the cart just once and get it when the user logs in again or create a new one when he use the app for first time, but I want to try it this way for now), is like if is not reading my code on that SessionController.
and for some reason even when I have my views this way:
app/views/customer/registrations
the changes that I do on that views are only reflected if I change it to
app/views/devise/registrations
my routes.rb is:
devise_for :clients, :controllers => { sessions: 'customers/sessions'}
devise_scope :client do
root to: "customers/Sessions#new"
end
the model that I am using with devise is Client
why I cant destroy the cart in the devise controller?
and why I cant use the views/customer/sessions if the documentation it says I can/have to do it?
thank you for reading.
you can always try to do
def destroy
cart = Cart.find(current_client.cart.id)
cart.destroy
super
end
but first you might want to ensure that you really overwritten devise's controller correctly.
The reason why you can't see changes done to app/views/customer/registrations is beacuse you seems to overwrite only :sessions controller, so you need to change
devise_for :clients, :controllers => { sessions: 'customers/sessions'}
to
devise_for :clients, :controllers => { registrations: 'customers/registrations', sessions: 'customers/sessions'}
The last question is:
" why I cant use the views/customer/sessions if the documentation it says I can/have to do it? "
You have a typo here, you are using customers namespace, not customer in routes.rb [ sessions: 'customers/sessions' ] - just a typo?
Watch your spelling. The before_filter is calling a method that doesn't exist.
I am using omniauth and found devise using a subfolder for this(in official example) controllers/users/omniauth_callbacks_controller.rb. I need to create a User show page as well as other actions for User so I decide to create a new UsersController inside a controllers/users folder. Now it looks like
class Users::UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
end
routes.rb
My::Application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
match 'users/:id' => 'users/users#show'
root :to => 'home#index'
end
it works but the route created is unnamed
rake routes gives
/users/:id(.:format) users/users#show
without GET and route_name
so I'm unable to use it for example after login redirect. Is there a better way to realize the subfolder routes structure and is it good idea to group controllers like this?
You just need name your route in your route.rb
match 'users/:id' => 'users/users#show', :as => 'user'
After that you can call this route by user_url(user.id)
See example on guides : http://guides.rubyonrails.org/routing.html#naming-routes