Devise with User model and STI, wrong controller on registration - ruby-on-rails

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.

Related

Change the default Devise GET user path

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)

Rails Subdomain for nested resources

I've got a Rails 4 app that I'm trying to reconfigure to use subdomains for jobportals. Right now the root path for each portal is /jobportals/:id. What I want is for a user to be able to go to client.example.com and hit that root path. Then for example, if the user edits their profile, the url would be client.example.com/user/edit rather than www.example.com/jobportals/:id/user/edit.
I followed this ASCIIcasts tutorial on setting up subdomain routing, but it's not working for me. When I go to http://client.lvh.me:3000/, I am hitting the root_path of my Rails app, rather than the root_path for the portal.
I think the problem is that Constraints(Subdomain) isn't working with resources :jobportals. How can I reconfigure my routes to accomplish what I'm after?
MyApp::Application.routes.draw do
devise_for :users, :controllers => {:registrations => "registrations"}
root 'general#home'
get '/about' => 'general#about'
get '/team' => 'general#team'
get '/careers' => 'general#careers'
get '/terms' => 'general#terms'
get '/privacy_policy' => 'general#privacy_policy'
constraints(Subdomain) do
resources :jobportals, controller: 'portals/general' do
member do
root 'portals/general#home'
devise_scope :user do
get '/user/sign_in' => 'portals/sessions#new'
post '/user/sign_in' => 'portals/sessions#create'
delete '/user/sign_out' => 'portals/sessions#destroy'
post '/user/password' => 'portals/passwords#create'
get '/user/password/new' => 'portals/passwords#new'
get '/user/password/edit' => 'portals/passwords#edit'
patch '/user/password' => 'portals/passwords#update'
put '/user/password' => 'portals/passwords#update'
post '/user' => 'portals/registrations#create'
get '/user/sign_up' => 'portals/registrations#new'
get '/user/edit' => 'portals/registrations#edit'
patch '/user' => 'portals/registrations#update'
put '/user' => 'portals/registrations#update'
delete '/user' => 'portals/registrations#destroy'
end
get '/jobs' => 'portals/general#jobs'
get '/companies' => 'portals/general#companies'
get '/alljobs' => 'portals/general#alljobs'
resources :applications, controller: 'portals/applications'
get ':id' => 'portals/companies#profile'
get ':id/jobs' => 'portals/companies#jobs'
get ':id/jobfunctions' => 'portals/companies#jobfunctions'
end
end
end
end
Working Code Below
MyApp::Application.routes.draw do
devise_for :users, :controllers => {:registrations => "registrations"}
root 'general#home'
get '/about' => 'general#about'
get '/team' => 'general#team'
get '/careers' => 'general#careers'
get '/terms' => 'general#terms'
get '/privacy_policy' => 'general#privacy_policy'
constraints(Subdomain) do
get '/' => 'portals/general#home'
devise_scope :user do
get '/user/sign_in' => 'portals/sessions#new'
post '/user/sign_in' => 'portals/sessions#create'
delete '/user/sign_out' => 'portals/sessions#destroy'
post '/user/password' => 'portals/passwords#create'
get '/user/password/new' => 'portals/passwords#new'
get '/user/password/edit' => 'portals/passwords#edit'
patch '/user/password' => 'portals/passwords#update'
put '/user/password' => 'portals/passwords#update'
post '/user' => 'portals/registrations#create'
get '/user/sign_up' => 'portals/registrations#new'
get '/user/edit' => 'portals/registrations#edit'
patch '/user' => 'portals/registrations#update'
put '/user' => 'portals/registrations#update'
delete '/user' => 'portals/registrations#destroy'
end
get '/jobs' => 'portals/general#jobs'
get '/companies' => 'portals/general#companies'
get '/alljobs' => 'portals/general#alljobs'
resources :applications, controller: 'portals/applications'
get ':id' => 'portals/companies#profile'
get ':id/jobs' => 'portals/companies#jobs'
get ':id/jobfunctions' => 'portals/companies#jobfunctions'
end
end
It is looking like constraints are not define properly, it is something like get 'jobportals', constraints: {subdomain: 'subdomain_name'}
Checkout the rails guide

Hot to redirect "example.com/////" to just "example.com" in Rails app?

My goal is when user requests root (AND ONLY ROOT, other pages work fine) with something like:
http://domain.com/////
http://domain.com//////////////
http://domain.com//
app should 301 redirect user to one and only correct:
http://domain.com/
I'm using Webrick, rack-rewrite gem and planning to host it on Heroku.
Please remember that i'm new in Rails.
UPDATE ======
Rails: redirect all unknown routes to root_url
— this is totally different problem. They just redirect all unknown hosts to root. I have 404 working normally.
My problem is that any amount of slashes works as homepage and URL has all of those slashes. I'm doing it for SEO to get rid of duplicates of homepage. I want it to work like here: http://netpeak.ua (try "netpeak.ua////////", you will be redirected to "netpeak.ua").
UPDATE 2 - added content of routes.rb ======
Rails.application.routes.draw do
resources :questions
resources :feedbacks
devise_for :users
get '' => 'public#index'
get '/agency' => 'public#about'
get '/contact' => 'public#contact'
get '/uslugi' => 'services#index'
get '/portfolio' => 'projects#index'
get '/uslugi/:id' => 'services#show'
get '/portfolio/:id' => 'projects#show'
resources :articles, param: :id
resources :settings, as: 'home' #home so it doesn't go to another page
namespace :admin do
resources :articles, :users, :projects, :services, :feedbacks, :questions
get '' => 'projects#index'
get 'contact' => 'settings#contact'
get 'feedback' => 'settings#feedback'
get 'fininfo' => 'settings#fininfo'
get 'calls' => 'settings#calls'
get 'orders' => 'settings#orders'
get 'letters' => 'settings#letters'
get 'allquestions' => 'settings#allquestions'
get 'projectsettings' => 'settings#projectsettings'
get 'servicessettings' => 'settings#servicessettings'
get 'aboutsettings' => 'settings#aboutsettings'
get 'startpagesettings' => 'settings#startpagesettings'
patch 'contact' => 'settings#update'
patch 'feedback' => 'settings#update'
patch 'fininfo' => 'settings#update'
patch 'projectsettings' => 'settings#update'
patch 'servicessettings' => 'settings#update'
patch 'aboutsettings' => 'settings#update'
patch 'startpagesettings' => 'settings#update'
end
end

Rails 4, Devise Routes

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.

Devise routing error not understand

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 %>

Resources