Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

Build a Devise Model in another controller

Gems / Libraries • Asked by Roman Storm

Lets say we have a Company model which accepts_nested_attributes_for User (devise Model)

How do we Save both a company and User model having that a Company has_many :users and a User belongs to a Company???

Users has :confirmable option as well.

so after successful creation of those, we should have company with company_id, a user with company_id as well assigned to him.


Yo! So I think having accepts_nested_attributes_for is really the main thing for making this work. The only other thing you really need is to make sure that your strong_params in the controller accepts those attributes.

What it will do is create or update the Company with its own attributes and then accepts_nested_attributes_for will automatically create the nested models for you (the Users) and it will also associate them too.

Probably the main thing to watch out for is that you need to also make sure the nested models are validated. Devise is going to require a password for the Users (by default) so you'll possibly need to set some default passwords for those users.

Are you having trouble making this work? Also the cocoon gem is great for creating those nested forms for the nested models. https://github.com/nathanvda/cocoon


Figured it out. It was the problem that I didn't permit and sanitized params, even though they were exactly the same in order to User.new user.save


Ah! That would do it! :)


Basically I did STI on User model, and made it working and then removed whatever I did, installed rolify + pundit :-)


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ 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.