Strong params on nested attributes in rails

user7567137 Source

I'm having a problem when specifying a nested param in the strong param whitelist

whitelisting with permit! works

def sign_up_params
  params.require(:user).permit!
end

but specifying the param fails

def sign_up_params
  params.require(:user).permit(:name, ......, role_ids: [])
end

error: unpermitted params role_ids: []

My models:

Roles: 
  has_and_belongs_to_many :users

Users:
  has_and_belongs_to_many :roles

join table: roles_users

habtm error

I added a console screenshot. It an habtm error (displaying like a link)

Thank you.. finally found an answer create

@user = User.new(sign_up_params)
@user.role_ids = params[:user][:role_ids]
ruby-on-railsrubyhas-and-belongs-to-many

Answers

answered 3 months ago Viet Nguyen #1

It seems have problem in your model relation. It need change to below:

users:
has_and_belongs_to_many :roles


roles:
has_and_belongs_to_many :users

comments powered by Disqus