我想有一个下降,由其他模型的时候,在索引页如何显示名称,而不是在轨
显示它的名字,我有3代表“生”,“教室的ID “和”classroom_students“ 我正在尝试的是当学生创建时,他应该能够从教室表中填充的下拉列表中添加教室,此时下拉工作正常,但是它正在从中获取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
下拉菜单或学生索引页面上的课堂标识问题在哪里? – Deep
我想你应该试试'student.classrooms.pluck(:classroom_name)'并遍历它们以表格格式显示名称 – Abhi
你的联系表示''学生有很多教室',但是你的数据库模式说你有'clasroom_id'也在桌子上。那么你想要什么? – Deep