0
votes

I have two different forms (two different controllers) and both these forms are using the Member model.

Devise uses it to create and update MyController uses it to create and update

My model validates all fields as required.

So an example:

I have MyController presenting a form with all the fields (it works fine) I have Devise presenting the register form with (email, password, password_confirmation).

When I try to submit the devise form, it shows me errors for fields that aren't there.

How can I skip validation for specific fields when Devise controller in the model?

1
What is your use case? Is MyModel your Devise Model? What do you mean by two controllers, did you override the Devise Registration controller (or any other controller)?rb512
@rb512 I have Devise installed and using the Member model, but I created a different controller for the user profile where I show all the other fields (edit and update) and I set them as required on the Member model. By default Devise uses (email, password and password_confirmation) on the register form, after I added this other fields to the model, when I try to register, Devise say that these fields are required... Basically devise try to validate fields that aren't in his form.Rafael Fragoso
ah, I see, please see my answer below.rb512

1 Answers

1
votes

It's good design to keep the devise model as small as possible and just keep it limited to user authentication. The reason being, every time you call current_user, it will load the whole object (with all the profile fields) whether or not you need them.

Since you've already created a new controller, I would suggest to create a new model for the profile attributes and create a has_one relationship with the devise model:

class User < ActiveRecord::Base
 require 'devise'
 has_one :user_profile
end

class UserProfile < ActiveRecord::Base
 belongs_to :user
 <user_profile fields>
end

With this approach your devise object will be lightweight and you can access the profile attributes only when required:

  current_user.user_profile