开发者

Rails devise when the user information belongs to Company devise

I have a created a devise for Companies. I have made a t开发者_开发问答able users, wish I want to store the user information password etc.. When a user sign up I want it to create a new user and a association to the Company.

My Company model: has_one: User My User model: Belongs_to: Company

How should I overwrite the registration controller?

Best regards, Rails beginner


Correct me if I'm wrong here, but it sounds like companies won't be logging in — only users, which belong to a company.

With that in mind, you should have User as the Devise model instead of Company. Your associations, then, are correct. The next step would be to add fields for the user in the form for company#new using nested attributes (fields_for in the view; accepts_nested_attributes_for in the model).

Answering your original question: you can use nested attributes to create a new user in the sign up form. You wouldn't need to override Devise's registration controller. Just modify the sign up form to include fields_for a user. Also, make sure the Company model has accepts_nested_attributes_for for the User model.

A quick aside: I'm not aware of the architecture of your application, but it seems like a has_many would be more appropriate. In cases similar to this, it's far more likely that you'll want to have multiple users belonging to a single company.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜