我正在尝试从其他模型下拉并在索引页面中显示其名称
我有3个表“学生”,“教室”和“教室_学生” 我正在尝试的是,当一个学生被创建时,他应该能够从一个从教室桌子填充的下拉列表中添加教室,在下拉工作的那一刻,但它从下拉列表获得id
如何让课堂名称显示在索引页面
课堂模式
class Classroom < ApplicationRecord
belongs_to :user
has_many :classroom_students
has_many :students, through: :classroom_students
end
学生模型
class Student < ApplicationRecord
has_many :classroom_students
has_many :classrooms, through: :classroom_students
validates :student_fname, presence: true, length: { minimum: 3, maximum: 50 }
end
classroom_student模型
class ClassroomStudent < ApplicationRecord
belongs_to :classroom
belongs_to :student
end
学生控制器
def student_params
params.require(:student).permit(:student_fname, :student_lname, :gender, :dob, :aboriginal, :esl, :special_provisions, :user_id, :classroom_id, :group_id, :active)
end
教室控制器
def classroom_params
params.require(:classroom).permit(:classroom_name, :classroom_year, :classroom_student)
end
学生观点表
<%= form.select :classroom_id, Classroom.where(:user_id => current_user.id).map {|r| [r.classroom_name, r.id]} %>
学生索引
目前它是id,但我希望它是教室名称
<td><%= student.classroom_id %>
架构文件
create_table "classroom_students", force: :cascade do |t|
t.integer "classroom_id"
t.integer "student_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "classrooms", force: :cascade do |t|
t.string "classroom_name"
t.date "year"
t.string "classroom_student"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "users_id"
t.integer "user_id"
t.string "classroom_year"
t.index ["users_id"], name: "index_classrooms_on_users_id"
end
create_table "students", force: :cascade do |t|
t.string "student_fname"
t.string "student_lname"
t.boolean "gender"
t.string "dob"
t.boolean "aboriginal"
t.boolean "esl"
t.text "special_provisions"
t.integer "user_id"
t.integer "classroom_id"
t.integer "group_id"
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "classroom_name"
end
答案 0 :(得分:1)
您的关联表示student has many classrooms
,但您的数据库架构显示clasroom_id
表中也有students
。如果您需要将classroom_id
与多个student
相关联,请先删除classrooms
。然后你的选择说:
<%= form.select :classroom_id, Classroom.where(:user_id => current_user.id).map {|r| [r.classroom_name, r.id]} %>
现在您有多个classrooms
与一个student
相关联,因此这不起作用,因为classroom_id
无效。相反它应该是这样的:
<%= f.select :classroom_ids, Classroom.where(user_id: current_user.id).map { |r| [r.classroom_name, r.id] }, {}, multiple: true %>
这会将多个classrooms
与student
相关联。
并且不要忘记在强大的参数中添加classroom_ids
:
def student_params
params.require(:student).permit(:student_fname, :student_lname, :gender, :dob, :aboriginal, :esl, :special_provisions, :user_id, :classroom_id, :group_id, :active, :classroom_ids => [])
end
希望这有帮助。
答案 1 :(得分:0)
首先,你有一个用户has_many:教室的关联,所以你不能认为你只有一个教室。
您可以使用以下内容获取与用户关联的所有类:
<%= f.form_for @student do |f| %>
<%= f.collection_select(:classrrom_id, Classroom.all, :id, :classroom_name,
{:prompt => "Select your classrooms"}, {:multiple => true}) %>
<% end %>
在您的students_controller.rb中,您应该将lesson_ids添加到您的参数
def student_params
params.require(:student).permit(:student_fname, :student_lname, :gender,
:dob, :aboriginal, :esl, :special_provisions, :user_id, :classroom_id,
:group_id, :active, :classroom_ids => [])
end