强参数不使用simple_form将属性列入白名单

时间:2016-04-08 18:41:02

标签: ruby-on-rails simple-form strong-parameters

我在理解如何嵌套3个模型时遇到了一些问题。我试图在关系的最深处向Video添加WorkoutSteps(不创建新视频,但从下拉列表中选择现有视频)

型号:

Workout WorkoutSet WorkoutStep - >只是一个Video的数组 Video

关系 锻炼: 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

以下是观点:

_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

检查参数:

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

1 个答案:

答案 0 :(得分:0)

main_video_ids是一个数组属性,而不是id的散列。所以你可以直接将它列入白名单:

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