Ruby on rails 使用强参数匹配集合\u选择输出
在rails控制台中&使用以下模型,我将K、1和2年级连接到学校,其编辑表单具有此选择字段: 如您所见,该关联正确地选择了字段中的3个项目,但如果我单击以选择/取消选择成绩,则不会保存这些更改 以下是模型:Ruby on rails 使用强参数匹配集合\u选择输出,ruby-on-rails,has-and-belongs-to-many,form-for,strong-parameters,collection-select,Ruby On Rails,Has And Belongs To Many,Form For,Strong Parameters,Collection Select,在rails控制台中&使用以下模型,我将K、1和2年级连接到学校,其编辑表单具有此选择字段: 如您所见,该关联正确地选择了字段中的3个项目,但如果我单击以选择/取消选择成绩,则不会保存这些更改 以下是模型: # app/models/school.rb class School < ActiveRecord::Base has_many :grades_schools, inverse_of: :school has_many :grades, through: :grades_s
# app/models/school.rb
class School < ActiveRecord::Base
has_many :grades_schools, inverse_of: :school
has_many :grades, through: :grades_schools
accepts_nested_attributes_for :grades_schools, allow_destroy: true
end
# app/models/grades_school.rb
class GradesSchool < ActiveRecord::Base
belongs_to :school
belongs_to :grade
end
# app/models/grade.rb
class Grade < ActiveRecord::Base
has_many :grades_schools, inverse_of: :grade
has_many :schools, through: :grades_schools
end
表单如下所示:
# app/views/schools/_form.html.haml
= form_for(@school) do |f|
/ <snip> other fields
= collection_select(:school, :grade_ids, @all_grades, :id, :name, {:selected => @school.grade_ids, include_hidden: false}, {:multiple => true})
/ <snip> other fields + submit button
# app/controllers/schools_controller.rb
class SchoolsController < ApplicationController
before_action :set_school, only: [:show, :edit, :update]
def index
@schools = School.all
end
def show
end
def new
@school = School.new
@all_grades = Grade.all
@grades_schools = @school.grades_schools.build
end
def edit
@all_grades = Grade.all
@grades_schools = @school.grades_schools.build
end
def create
@school = School.new(school_params)
respond_to do |format|
if @school.save
format.html { redirect_to @school, notice: 'School was successfully created.' }
format.json { render :show, status: :created, location: @school }
else
format.html { render :new }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @school.update(school_params)
format.html { redirect_to @school, notice: 'School was successfully updated.' }
format.json { render :show, status: :ok, location: @school }
else
format.html { render :edit }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
private
def set_school
@school = School.find(params[:id])
end
def school_params
params.require(:school).permit(:name, :date, :school_id, grades_attributes: [:id])
end
end
控制器如下所示:
# app/views/schools/_form.html.haml
= form_for(@school) do |f|
/ <snip> other fields
= collection_select(:school, :grade_ids, @all_grades, :id, :name, {:selected => @school.grade_ids, include_hidden: false}, {:multiple => true})
/ <snip> other fields + submit button
# app/controllers/schools_controller.rb
class SchoolsController < ApplicationController
before_action :set_school, only: [:show, :edit, :update]
def index
@schools = School.all
end
def show
end
def new
@school = School.new
@all_grades = Grade.all
@grades_schools = @school.grades_schools.build
end
def edit
@all_grades = Grade.all
@grades_schools = @school.grades_schools.build
end
def create
@school = School.new(school_params)
respond_to do |format|
if @school.save
format.html { redirect_to @school, notice: 'School was successfully created.' }
format.json { render :show, status: :created, location: @school }
else
format.html { render :new }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @school.update(school_params)
format.html { redirect_to @school, notice: 'School was successfully updated.' }
format.json { render :show, status: :ok, location: @school }
else
format.html { render :edit }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
private
def set_school
@school = School.find(params[:id])
end
def school_params
params.require(:school).permit(:name, :date, :school_id, grades_attributes: [:id])
end
end
我觉得我的问题的症结在于collection_select生成的参数与强参数之间的不匹配。其中一个或两个可能都不正确,但我一生都无法在网上找到示例代码来说明我做错了什么
在尝试了大量失败的变体之后,我已经不知所措了!提前感谢您的帮助 废话。我可以发誓我以前试过,但一定是在表单中使用字段而不是集合时。解决方案:
def school_params
params.require(:school).permit(:name, :date, :school_id, grades_attributes: [:id])
end
变成
def school_params
params.require(:school).permit(:name, :date, :school_id, grade_ids: [])
end
我仍然很好奇在@grades\u学校使用fields\u时,它会如何工作,但我不得不把调查留到下一天