Skip to main content

Devise with additional user fields, nested models / attributes and custom routes

Gems / Libraries • Asked by Dan Tappin

I have posted a few related questions on this but now I am at a road block. I had this system working quite well and then I went to add to it and now I broke it real good!

First off I just discovered Gist so here is my code:

https://gist.github.com/jasper502/065a63fde266fac1fbf5

The idea here is that I have a single User login that can eventually be tied to multiple Companies via the Role.

You signup via Devise on the custom /register route and fill out the Company information etc. The custom registration controller creates the Role during the user creation and sets a few other attributes.

When I try to create a new user / company the additional user fields (name_first & name_last) always fail validation regardless if they are in fact valid. The nested Company fields do not validate at all. If I enter the email and password field only the form works but only creates the User record.

To me it seems like the custom registration controller is not being processed at all because it seems to not want to allow my additional params specified in sign_up_params


Hmmm... took my routes and tried this for fun:

devise_for :users, :skip => [:sessions], :controllers => { :registrations => 'users/registrations', :sessions => 'users/sessions' }
as :user do
get '/signin' => 'devise/sessions#new', :as => :new_user_session
post '/signin' => 'devise/sessions#create', :as => :user_session
delete '/signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end

I wanted to see if the normal Devise routes work and they do except that now the validation fails completely. If I submit a blank form I get all kinds of errors but if I fill it out it works fine now. I am getting close but obviously missing something small here.


I think I figured it out. I stumbled across this:

https://gist.github.com/6dd44fc841e80bbc2265.git

I switched my routes to "user/..." from "devise/..." and that seemed to do the trick.


Oh nice. I rarely tend to customize Devise routes so it has been a while since the last time I did it. Glad the gist you found worked!


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.