Devise to show user id in the URL? - ruby-on-rails

I am using Devise for authentication and I want to have URLs of the form /user/:id but I am not sure how to do this.
I guess I need to edit the routes file but not sure what change to make? Is there a simple way to do this?
My routes.rb file looks like this:
devise_for :users, :controllers => {:registrations => 'registrations'}
Thanks

Related

Devise Registration URL could not be found

I have a problem with devise after customizing the signup - route.
The devise doc mentions, that routes can easily be customized, so I tried to add a token to the URL to set up a easy invitation system. Ist really straight forward and all I did was adding
devise_for :users, :path_names => { :sign_up => "signup/:invitation_token" }
to my routes. A mailer sends an email with the token and inside I pass
new_user_registration_path(#invitation.token)
rake routes says
new_user_registration GET /users/signup/:invitation_token(.:format) devise/registrations#new
But I'm still getting
No route matches {:action=>"new", :controller=>"devise/registrations", :locale=>:de, :invitation_token=>nil}
I get it wether I pass the token or not...
I'm not shure what I'm missing.
Thanks in advance, hope someones sees what I'm doing wrong.
Greets, Rob
Check #invitation.token to make sure that it's not nil.
The error you're witnessing will occur on any view in which you pass nil into your new_user_registration_path link tag.
Bear in mind that you'll need to override the default behavior of Devise's users/registration controller in order to get your invitation system working correctly. Something like this would work:
# routes.rb
devise_for :users, :path_names => { :sign_up => "signup/:invitation_token" }, :controllers => {:registrations => "users/registrations"}
# app/controllers/users/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
# add custom create logic here
end
end

Devise authentication as API only

I'm trying to use Devise authentication with Angular.js. Everything is already working except I want to hide the server-side login form, i.e. the result of /users/sign_in GET request leaving only the possibility of /users/sign_in POST request. Is this possible?
I think you'll need to skip sessions in your devise_for call in routes.rb (or not even call it) and just stick to devise_scope for setting up your sign_in path. So, your routes.rb would look like this:
devise_for :users, :skip => :sessions
devise_scope do
post "/users/sign_in" => "devise/sessions#create"
delete "/users/sign_out" => "devise/sessions#destroy"
end

Devise & Custom Rails User URLs

I am currently using Rails 3.2.5 with the latest devise gem.
Currently users can access their profile page at...
example.com/users/john-doe
I want to remove the users portion of the url, so the url would be example.com/john-doe. Is this possible with devise?
Right now in my routes file I have the following...
devise_for :users, :controllers => {:omniauth_callbacks => 'omniauth_callbacks'}
Just add a route for it. You will probably want it to be the last one in the routes file.
Something like:
match '/:user' => "users#show", :as => :user_profile

Configuring routes in devise when only using omniauth for authentication

I have built an application which allows a user to authenticate against Active Directory using omniauth-ldap. If this is a new user, the successful authentication creates a user for them based on information returned from AD. If the user already exists, it just logs them in. Users do not register for the application, they just log in with AD credentials. And I never want the user to log in with database credentials.
I can't figure out how to get rid of or change around some of the routes. For example if a user visits /sign_in they get the database authentication. And if the user visits sign_up they are taken to a page to register for the site. I would like for users that visit /sign_in to be taken to the LDAP login which is /users/auth/ldap. I think I need to make a custom route, but I'm not sure which controller I need to direct the user to. And I want to make the sign_up page go away entirely.
Right now I have a link that allows users to log in using ldap, and the path for that is user_omniauth_authorize_path(:ldap). I'm just not sure how to translate that into something that my config/routes.rb file understands. This is what I have in routes right now.
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
When I run rake routes I do not see any route for user_omniauth_authorize_path which I presume is because that route is being generated by devise. So I think I need to have my routes point to a devise controller, but I can't seem to find the right path.
Try to add
:skip => [:sessions, :registrations] to your routes.rb
Something like this:
devise_for :users, :skip => [:sessions, :registrations]
This How To article might be helpful, and also here is one more link to go through.

Devise change the user_omniauth_callback route

I am working with OmniAuth to use Facebook Connect in my Devise based rails app. One of the routes it creates is:
user_omniauth_callback /users/auth/:action/callback(.:format) {:action=>/facebook/, :controller=>"devise/omniauth_callbacks"}
I'd like to modify this route to a custom URL. Where would be the right place to do that?
the problem is by default, the route it creates is http://foo/users/auth/:action/callback.format. I want to have something more custom like http://foo/prefix_path/users/auth/:action/callback.format. I tried making my routes file look like the following:
scope "/mypath" do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
end
but it still generates the wrong route:
user_omniauth_callback /users/auth/:action/callback(.:format) {:action=>/facebook/, :controller=>"users/omniauth_callbacks"}
I'm not exactly sure what you are asking, I assume you want to have your own custom code for the callback.
You can extend the devise controller such as:
class MyOmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
#Custom Code here
end
end
Then you can register this new controller in your routes.rb
devise_for :users, :controllers => {:omniauth_callbacks => "my_omniauth_callbacks"}
EDIT:
devise can also take a 'path' option in the devise_for so changing the route:
devise_for :users, :controllers => {:omniauth_callbacks => "my_omniauth_callbacks"}, :path => "path_prefix/users"
If you are unsatisfied with omniauthable in devise itself, then you may consider implementing omniauth as separate gem and then just tie it with device.
To modify routes, you may use :match as well and map those routes to omniauth_callbacks url. Didn't get why you want to
I'd like to modify this route to a custom URL.
Decribe what you want to make different that what is available.

Resources