简体   繁体   中英

Strong params on nested attributes in rails

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM