Devise/Rails: after_sign_up_path_for(resource) - devise

I am using devise in my rails app to user sign up/in. On sign up, i want to redirect user to its profile page.
I did rake routes and for edit, root is :
/profiles/:id/edit
I wrote this in registrations controller:
def after_sign_up_path_for(resource)
'http://localhost:3000/profiles/#{resource.id}/edit'
end
But it is not working. Can anybody help?

shouldn't you use:
def after_sign_up_path_for(resource)
edit_profile_path(resource)
end
instead of hardcoded http://localhost:3000/profiles/#{resource.id}/edit
would be helpfull to have your routes file posted too.

Related

Devise: "def create" in users_controller.rb not working?

I've been pulling my hair out trying to get anything working with "def create" and "def update" in the users_controller.rb for Devise.
For instance, I've tried this:
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
flash[:notice] = "Test Save"
else
flash[:notice] = "Test Error"
end
end
end
I've used this code along with the appropriate code to show flash notices in the views section. However nothing is shown when I either submit a blank form, an incomplete form, or a complete form. The user registration will still go through on a complete form, but it does not follow anything I put in "def create". I've tried other ways of testing this aside from flash notices, such as sending to a different page, etc. I get no response. The same thing for "def update", it doesn't seem to even use that code.
I'm completely dumbfounded on this one, any ideas?
If i understand your question correctly, you should be overwriting the devise controller.
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
# add custom create logic here
end
def update
super
end
end
You can see what the default devise controllers are doing here:
https://github.com/plataformatec/devise/tree/master/app/controllers/devise
If you just want to edit the flash message, looking at the link above shows that devise uses a method called set_flash_message
# Sets the flash message with :key, using I18n. By default you are able
# to setup your messages using specific resource scope, and if no one is
# found we look to default scope.
# Example (i18n locale file):
#
# en:
# devise:
# registrations:
# signed_up: 'Welcome! You have signed up successfully.'
So you can just edit your devise.en.yml file with the correct text and voila!
Note: If you do overwrite the controller don't forget to also add
# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
How about this instead?
if #user.save
redirect_to #user, notice: 'User was successfully created.'
else
render action: 'new'
end
You are setting the flash, but no redirection and no rendering. I'm wondering if you are getting a blank page, or a 200 with no body.
This will redirect to the show action, setting a flash notice if successful and render the new form with the #user.errors showing why it failed.
If you are using devise, you could use the Registrations Controller to create a new account, you shouldn't need to create a new one. If you create a new one, there might be a conflict in the routes with registrations#create and users#create both pointing to POST /users

Active admin change default model admin_user

I'm starting my first project with Active Admin.
To use another model for my users I use the following command :
rails generate active_admin:install User
After this I make this change in active_admin initializer :
config.authentication_method = :authenticate_user!
config.current_user_method = :current_user
I'm correctly login my application but on the home page I get this error :
undefined method `destroy_admin_user_session_path' for #<ActiveAdmin::Views::HeaderRenderer:0x007ff8fa086a60>
How can I fix it properly ?
Solved by editing initializer :
config.logout_link_path = :destroy_user_session_path
This is addition to #Awea answer. Use togather with that.
Check rails routing table for destroy_user_session.
For example devise token auth make route table entry like this:
destroy_user_session DELETE /auth/sign_out(.:format) devise_token_auth/sessions#destroy
But default method for activeadmin logout link is :get and it will not work.
To make it worked properly add to config/initializers/active_admin.rb also and:
config.logout_link_method = :delete

Login to other user's account with Devise+Active Admin+Switch User

I'm trying to implement switch_user gem in my existing rails 3.0.9 application.
There are two models on my application, they are
User - for my customer accounts and it has_one Account
AdminUser - This was created by ActiveAdmin
I have already enabled devise authentication for Users and ActiveAdmin also working pretty much well with AdminUser. Now from my Active Admin interface I'd like to select the Accounts and login to those account just like the account owner does. Switch user is working fine but the problem is anyone can simply login to the user accounts now if they know the urls.
http://localhost:3000/switch_user?scope_identifier=user_1
All I need is allow only an AdminUser (i.e if there is an ActiveAdmin session) to access the User's accounts.
This is how my /config/initializers/switch_user.rb looks like
SwitchUser.setup do |config|
config.controller_guard = lambda { |current_user, request| current_admin_user.nil?}
config.redirect_path = lambda { |request, params| "/dashboard" }
end
But I get this error
NameError in SwitchUserController#set_current_user
undefined local variable or method `current_admin_user' for main:Object
Is there anyway I can access the active admin session?
Code for /config/initializers/active_admin.rb
ActiveAdmin.setup do |config|
config.site_title = "MyAppName"
config.authentication_method = :authenticate_admin_user!
config.current_user_method = :current_admin_user
end
btw in my application controller I haven't created any methods for authenticate_admin_user , current_admin_user active admin works fine without them.
You need modify local config/initializers/switch_user.rb:
config.controller_guard = lambda { |current_user, request, original_user, controller|
controller.admin_user_signed_in?
}
Original lambda has 2 arguments.
Just append more (up to 4) and use it.
Don't forget restart rails server :)
OK I think I found a solution to secure the switch_user. All I did is moving the routes inside the admin_users scope
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config do
match '/admin/switch_user', :controller => 'switch_user', :action => 'set_current_user'
end

Change devise route from ID to Username

I have tried using the post to change the routes to point at the username. I know this is simple, but for some reason I can't compute it at the moment. I have tried everything on the devise documentation page as well.
Drawing username routes
I just want to have the routes layout to use the username instead of id and not have the users prefix. Like:
http://example.com/username
instead of
http://example.com/users/1
class User < ActiveRecord::Base
def to_param
username
end
end
in your controller make
#user = User.find_by_username(params[:id])
instead of
#user = User.find(params[:id])
this will make your routes like http://example.com/users/username
to make what you want, you can do route like:
resources :users, :path => '' do
# nested resources...
end
so, user_path(#user) will make url http://example.com/username
but It's not a good practice, cause it's not a REST. I advise you to leave urls like http://example.com/users/username

Subdomains rails 3

I have a simple question I can't figure out.
I have a rails 3 app using subdomains.
A firm have many users. When a user log in, I want to redirect them to their firms subdomain.
I've used Ryan Bates screencast to get subdomains working.
http://railscasts.com/episodes/221-subdomains-in-rails-3
In my user_sessions_controller I have.
def create
#user_session = UserSession.new(params[:user_session])
if #user_session.save
#firm = current_user.firm
flash[:notice] = "Successfully logged in."
redirect_to root_url(:subdomain => #firm.subdomain)
else
render :action => 'new'
end
end
This sends the user in the firm with subdomain lizz to this url
http://lvh.me:3000/?subdomain=lizz
when the user is logged in this link works
<%= link_to current_firm.subdomain, root_url(:subdomain => current_firm.subdomain) %>
Do you have any ideas on how to redirect from the controller to the subdomain?
I think you problem is that you are url the named url root_url. The helper method url_for that you modified (if you followed the Railscasts closely) is probably not used for the named url.
Try using url_for instead.
Edit
The names urls are generated in actionpack-3.0.x/lib/action_dispatch/routing/route_set.rb and will not use your custom url_for method. However, does support a :host parameter so you need to write something like
... root_url(:host => "#{current_firm.subdomain}.domain.tld") ...