I want to change the default path of some user (in this case with id = 1) information from:
domain.com/user.1
to
domain.com/user/1
I already use devise_for on my route.rb, is there some special command to do what I need with this?
this is my route.rb
devise_for :users,
:path => '',
:path_names => {
:sign_in => 'login',
:sign_out => 'logout',
:sign_up => 'register'
},
:controllers => { registrations: 'registrations' }
First, domain.com/user.1 doesn't look like a real route to me. Are you sure you're not calling users_path(id) when you intend to call user_path(id)?
Also, a user/:id route doesn't look like it has anything to do with Devise, which is concerned with authentication/authorization. It looks more like a show resource method that would go in UsersController#show.
In any case, the following route should give you the /user/:id route which maps to UsersController#show
resources :user
which would create the following route helper method:
user_path(id)
Related
I had User model for a while that worked with devise, now I have to add a new model to the app, called Profile, and as it has almost the same attributes as User I decided to inherit Profile from User.
user.rb
class User < ActiveRecord::Base
...
end
profile
class Profile < User
...
end
updated routes to work with devise:
devise_for :users, :path => '', :path_names => {:sign_up => "becometutor"},
:controllers => { :registrations => "users/registrations" }
devise_for :profiles, :path => '', :path_names => { :sign_up => "brand_profile"},
:controllers => { :registrations => "profiles/registrations" }
rake routes
user_registration POST / users/registrations#create
new_user_registration GET /becometutor(.:format) users/registrations#new
profile_registration POST / profiles/registrations#create
new_profile_registration GET /brand_profile(.:format) profiles/registrations#new
Problem
When I open new_profile_registration logs shows:
Processing by Profiles::RegistrationsController#new as HTML
when I click submit on form that should create a profile, logs shows:
Processing by Users::RegistrationsController#create as JS
Parameters: {"utf8"=>"✓", "profile"=>{"name"=>"", "phone"=>"", "owner_name"=>"", "owner_phone_number"=>""
Question
why create action points to Users::RegistrationController, instead of Profiles::RegistrationController ?
Thank you.
Fix - set path to be different for each resource
devise_for :users, :path => 'tutors', :path_names => {:sign_up => "becometutor"},
:controllers => { :registrations => "users/registrations" }
devise_for :profiles, :path => 'profiles', :path_names => { :sign_up => "brand_profile"},
:controllers => { :registrations => "profiles/registrations" }
You have 2 routes with the same url, user_registration POST / and profile_registration POST /, so Rails looks for the first, and it's point to users/registrations#create
I am not sure but i guess it is due routes . If you put profile route above user route , it might work.
You might be passing #user simply to the form_for and it is picking user create path as it comes first.
I've created custom routes to route to the devise login and logout paths:
devise_scope :admin do
get "logout" => "devise/sessions#destroy", as: :logout
get "login" => "devise/sessions#new", as: :login
end
This works. The only problem is that if the the login fails it redirects back to admins/sign_in instead of /login.
Any ideas?
According to this answer and this description, it seems the proper way to achieve what you're attempting to do is to make use of the :path_names option. According to the description from the Devise wiki:
devise_for :admin, :path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout'}
will create the normal admin routes for you, and will assign the /sign_in and /sign_out route to /login/ and /logout respectively.
Using the :path option, you can further alter the URL, such as using :path=>"admins" will yield routes like /admin/login, etc.
I'm using devise for rails.
I have the following route for devise.
devise_for :user
Which routes to 'user/sign_in' and several other.
So I want to change this route to: get 'login'. Is this possible?
I tried doing
match 'login', to: 'user/sign_in', via: :get
Which did not work as well, what am I doing wrong, and what does the above code do?
To use /login for sign_in add the following to your config/routes.rb:
devise_scope :user do
get 'login', to: 'devise/sessions#new'
end
This'll work:
devise_for :user, :path => 'login'
You might need :users and not :user, FYI.
I have this route file
Qitch::Application.routes.draw do
devise_for :users, :controllers => {
:omniauth_callbacks => "users/omniauth_callbacks",
:registrations => "users/registrations",
:sessions => "users/sessions",
:passwords => "users/passwords"
}
devise_for :users
as :user do
get '/sign_up', :to => "users/registrations#new"
get "sign_out", :to => "users/sessions#destroy"
end
root :to => 'welcome#index'
end
when i click on this link in application layout
Sign-up Now, It's fast and free
i have this error
Routing Error
No route matches {:controller=>"users/welcome"}
Try running rake routes for more information on available routes.
I don't understand why this occur
Any help
Thanks
1.) as the Routing Error promts, try to run rake routes which will show you all defined routes, from the output you can see if you defined something not as wanted
2.) as stated in devise custom routes try something like:
get "/sign_up" => "devise/registrations#new"
3.) use the paths in your view: generating paths and urls from code
<%= link_to "Login", signup_user_path %>
I made this HAML link:
= link_to "Create Profile", signup_path
I was thinking it should work since this url works:
http://m.cmply.local:8800/signup
But instead I get this error:
undefined local variable or method `signup_path' for #<#<Class:0x129a08190>:0x129a027e0>
Here is my routes.rb snippet
scope :module => :mobile, :as => :mobile do
constraints(:subdomain => /m/) do
devise_for :users, :path => "", :path_names =>
{ :sign_in => "login", :sign_out => "logout",
:sign_up => "signup" },
:controllers => {:sessions => "mobile/sessions"}
resources :home
resources :disclosures # Will have new, get, look up a disclosure
end
end
and here is the rake routes snippet
{:action=>"create", :controller=>"registrations"}
new_user_registration GET /signup(.:format)
Any idea why this might happen?
Thanks!
Change signup_path to new_user_registration_path
This is happening because rails does not know what "signup_path" is. I would recommend you run bundle exec rake routes and be sure that signup_path is actually in those routes. I can tell by looking at your routes.rb file that you're not going to have the signup_path route.
The route you are looking for is going to be something more like users_signup_path, because devise likes to wrap its routes in namespaces like that.
Try placing app_main.signup_path. It seems that is getting called on a different engine.
I've ran into this problem before.
And follow the directions of others such as to check your routes.