Alex.U
Alex.U

Reputation: 1701

Strong parameters not whitelisting attributes with simple_form

I'm having some issues understanding how to nest 3 models. I'm trying to, at the deepest point of the relations, add a Video to the WorkoutSteps (not creating a new video but select an existing one from a dropdown)

Models:

Workout WorkoutSet WorkoutStep -> just an array of Video Video

Relationships Workout: has_and_belongs_to_many :workout_sets, :join_table => :workout_sessions, dependent: :destroy

WorkoutSet has_and_belongs_to_many :workout_steps, :join_table => :sets_steps, dependent: :destroy

WorkoutStep has_and_belongs_to_many :workout_sets, :join_table => :sets_steps

And the following in the views:

_form.html.haml

= simple_form_for(@workout, url: admin_workouts_path(@workout)) do |f|
  = f.input :title
  %h3 Sets
  .sets.some{ :style => "margin-left: 25px" }
    = f.simple_fields_for :workout_sets do |set|
      = render 'workout_set_fields', f: set
    .links
      = link_to_add_association 'add set', f, :workout_sets
  = f.submit

_workout_set_fields

= f.label :title
= f.text_field :title
%br
%br
  #sets.some{ :style => "margin-left: 25px" }
    = f.simple_fields_for :workout_steps do |step|
      = render 'workout_step_fields', f: step
    .links
      = link_to_add_association 'add step', f, :workout_steps

_workout_step_fields

= f.association :main_videos, include_hidden: false

workouts_controller.rb

def workout_params
    params.require(:workout).permit(:title, :pro, :workout_step_id, workout_sets_attributes: [ :id, :_destroy, :title, workout_steps_attributes: [ main_video_ids: [:id] ] ])
end

Checking the params:

screen shot 2016-04-08 at 8 34 51 pm

Upvotes: 2

Views: 682

Answers (2)

Alex.U
Alex.U

Reputation: 1701

FYI, the problem was solved as I stated on the comment above.

params.require(:workout).permit(:title, :pro, :workout_step_id, workout_sets_attributes: [ :id, :_destroy, :title, workout_steps_attributes: [ {main_video_ids: []} ]])

for more info, check this link

Upvotes: 1

Anthony E
Anthony E

Reputation: 11235

main_video_ids is an array attribute, not a hash of ids. So you can whitelist it directly:

def workout_params
    params.require(:workout).permit(:title, :pro, :workout_step_id, workout_sets_attributes: [ :id, :_destroy, :title, workout_steps_attributes: main_video_ids: [] ] ])
end

Upvotes: 0

Related Questions