我正在尝试同时创建许多缺席(针对不同的学生),而且我对rails非常陌生。我不断收到“表格中的第一个参数不能包含nil或为空”的错误。
我也想知道我是否需要一种新方法。
这是我的表格
= form_for @absent, url: absent_path, method: :post do |f|
table.table
thead
tr
th Name
th Absent
tbody
- @students.each do |student|
tr
td = student.full_name
td = f.check_box :student_ids, student.id, @absent.students.include?(student), name: "absent[student_ids][]", id: "student_id_#{student.id}"
= f.submit 'Save'
这是我的控制器
def new
@absent = Absent.new
end
def create
absent_params[:student_ids].each do |student_id|
@absent = Absent.new(student_id: student_id)
if @absent.save
redirect_to absent_path
else
render :index
end
end
end
private
def absent_params
params.require(:absent).permit(
:student_ids
)
end
这是我的架构:
create_table "absents", force: true do |t|
t.integer "student_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "absents", ["student_id"], name: "index_absents_on_student_id", using: :btree
create_table "students", force: true do |t|
t.string "full_name"
t.datetime "created_at"
t.datetime "updated_at"
end
以下是我的模特:
class Student < ActiveRecord::Base
has_many :absents, foreign_key: :student_id
end
class Absent < ActiveRecord::Base
belongs_to :student, foreign_key: :student_id
end