Update many_to_many association in nested form

Update many_to_many association in nested form



I have three models for the context of this question. And using the wicked gem for multi-step signup.


wicked


class User < ApplicationRecord
has_one :profile
has_many :specialties, through: :profile

accepts_nested_attributes_for :profile, allow_destroy: true
end


class Profile < ApplicationRecord
belongs_to :user
has_many :profile_specialties
has_many :specialties, through: :profile_specialties
end


class Specialty < ApplicationRecord
has_many :profile_specialties
has_many :profiles, through: :profile_specialties
end



In my form, I pass specialty_ids


specialty_ids


<%= simple_form_for @user, url: wizard_path do |f| %>

...

<%= simple_fields_for :profile_attributes do |cf| %>

<%= cf.input :specialty_ids, collection: Role.order(:name), as: :grouped_select, group_method: :specialties, input_html: multiple: true %>

<% end %>

...

<% end %>



to the AfterSignup#update


AfterSignup#update


def update
@user = current_user
@user.update_attributes(user_params)
render_wizard @user
end



I believed that rails handled the update of associations through naming. But maybe I'm mistaken and need to explicitly update the associations in the controller. Or perhaps I've not named items correctly...



Either way, I'm a little unclear on why the profile specialties don't update.



When I try to do a update, the console logs Unpermitted parameters: :specialty_ids


Unpermitted parameters: :specialty_ids



I have strong params defined as such


def user_params
params.permit profile_attributes: [..., :specialty_ids]
end



Here is the full log


Processing by AfterSignupController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "profile_attributes"=>"specialty_ids"=>["", "22"], "commit"=>"Save and Continue", "id"=>"step_one"
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 52], ["LIMIT", 1]]
Unpermitted parameters: :specialty_ids
Unpermitted parameters: :utf8, :_method, :authenticity_token, :commit, :id
(0.2ms) BEGIN
Profile Load (0.2ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT $2 [["user_id", 52], ["LIMIT", 1]]
SQL (0.4ms) DELETE FROM "profiles" WHERE "profiles"."id" = $1 [["id", 110]]
SQL (0.4ms) INSERT INTO "profiles" ("user_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["user_id", 52], ["created_at", "2018-09-06 02:30:57.882173"], ["updated_at", "2018-09-06 02:30:57.882173"]]
(0.5ms) COMMIT
(0.2ms) BEGIN
(0.2ms) COMMIT
Redirected to http://localhost:3000/after_signup/requirements






What is the entire console message, there is important info in there. What do the params you are trying to pass LOOK like in the console? Is params expecting an array of IDs?

– Beartech
Sep 6 '18 at 3:48






@Beartech I've added the full message

– jordan
Sep 6 '18 at 3:55






params[:speciality_ids] is an array. Try params.permit profile_attributes: [..., specialty_ids: ]

– Jagdeep Singh
Sep 6 '18 at 4:00


params[:speciality_ids]


params.permit profile_attributes: [..., specialty_ids: ]




1 Answer
1



Your strong params is expecting an array so you need:


def user_params
params.permit profile_attributes: [..., specialty_ids: ]
end






ah, there it was. much more simple than I expected.

– jordan
Sep 6 '18 at 4:07






Params can be a pain sometimes.

– Beartech
Sep 6 '18 at 4:16



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)