Reputation: 41949
The code in this question works without issue in Rails 3, but it's raising a ActiveModel::MissingAttributeError in rails 4, which also uses Devise (for Rails 4). There are three relevant models, with a polymorphic relationship between the User model and the other two.
User.rb
belongs_to :profile, polymorphic: true
LawyerProfile.rb
has_one :user, as: :profile, dependent: :destroy
LawStudentProfile.rb
has_one :user, as: :profile, dependent: :destroy
After the user fills out the relevant registration form, a new lawyer or student profile is created for the new user inside the after_sign_up_path_for(user) method from Devise that I've rewritten inside the registrations controller, like this
class RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(user)
if user.lawyer === true
user.profile = LawyerProfile.create!
user.add_role :lawyer
user.save!
elsif user.student === true
user.profile = LawStudentProfile.create!
user.add_role :student
user.save!
else
...
I'm getting an error on this line (on a lawyer sign up)
user.profile = LawyerProfile.create!
with this error message
ActiveModel::MissingAttributeError in RegistrationsController#create
can't write unknown attribute `profile_id'
As I had this working on a Rails 3 app, I'm guessing this might be related to strong parameters, however, I never had to add profile_id to a list of attr_accessible
in the Rails 3 app, so I'm not entirely certain it's a strong parameters issue.In fact, neither the user nor the LawyerProfile model has an attribute (that I created or which is otherwise visible) called profile_id
Anyways, inside the Application controller, per Devise instructions, I added the following which creates the whitelist of accepted parameters for user on signup. I added :profile_id to this list but I still got the same error.
before_filter :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :lawyer, :student, :password) }
end
Not being entirely clear about polymorphic associations, I thought maybe :profile_id is an attribute of LawyerProfile (even though i didn't add it to LawyerProfile.rb attr_accessible
in the Rails 3 app). There is no create action in my lawyer_profiles controller.
Questions
1) if the reason for the error message is that I have not created a whitelist of attributes (i.e. :profile_id) for the LawyerProfile.rb model, how do I do that in the lawyer_profiles_controller if I don't have a create action? I only use LawyerProfile.create! the one time.
2) what might be another reason for this error if it's not the reason alluded to in the first question?
Update,
One odd aspect of this is that neither the User model nor the LawyerProfile model have a profile_id
column. They both just have id columns, as was the case in the Rails 3 version.
Upvotes: 2
Views: 2309
Reputation: 41949
Answering my own question. Even though the code was exactly the same between the Rails 3 and the Rails4 app, there was an error raised in the Rails 4 app because I did not have a profile_id (or profile_type) column on the users table. Since I wasn't saving it to the db in the Rails 3 app, it didn't raise an error. I wasn't saving it to the db in the Rails 4 app either but Rails now raises an error in this situation.
Upvotes: 1