我当然有模型,每个课程有很多单元,每个单元有很多视频,都有一个测验,最后每个测验都有很多问题。
当我尝试添加新课程或编辑一门课程时,这是我得到的错误:
#的未定义方法“ has_one” 你的意思是? has_many
这是我的模特
Course.rb
class Course < ApplicationRecord
validates_presence_of :title
validates_presence_of :author
has_many :units, dependent: :destroy, autosave: true
accepts_nested_attributes_for :units, allow_destroy: true
acts_as_paranoid
end
Unit.rb
class Unit < ApplicationRecord
validates_presence_of :name
validates_presence_of :course_id
belongs_to :course
has_many :videos, dependent: :destroy, autosave: true
has_one :quiz, dependent: :destroy, autosave: true
accepts_nested_attributes_for :videos, allow_destroy: true
accepts_nested_attributes_for :quiz, allow_destroy: true
acts_as_paranoid
end
Video.rb
class Video < ApplicationRecord
validates :data ,presence: true
validates_presence_of :unit
validates_presence_of :unit_id
validates_presence_of :name
belongs_to :unit
acts_as_paranoid
end
Quiz.rb
class Quiz < ApplicationRecord
validates_presence_of :name
validates_presence_of :unit
validates_presence_of :unit_id
belongs_to :unit
has_many :questions, :dependent => :destroy, :autosave => true
accepts_nested_attributes_for :questions, allow_destroy: true
acts_as_paranoid
end
Question.rb
class Question < ApplicationRecord
validates :question ,presence: true
validates :answer1 ,presence: true
validates :answer2 ,presence: true
validates :answer3 ,presence: true
validates :answer4 ,presence: true
validates :correct ,presence: true
validates_presence_of :segment
belongs_to :quiz
acts_as_paranoid
end
这是活动管理员中的courses.rb:
ActiveAdmin.register Course do
permit_params :id, :title, :author, :course_details, :course_expectations, :course_time, :course_topics, :course_additional_resources,
units_attributes: [:id, :name, :_destroy, :_create, :_update,
videos_attributes: [:id, :name, :data],
quiz_attributes: [:id, :name,
questions_attributes: [:id, :question, :answer1, :answer2, :answer3, :answer4, :correct, :_destroy, :_create, :_update]]]
config.sort_order = 'id_asc'
active_admin_paranoia
# /admin/course/:id/units
member_action :units do
@units = resource.units
# This will render app/views/admin/courses/units.html.erb
end
index do
column :id
column :title
column :author
actions
end
show do
panel "About this course" do
div do
label "Course Topics:"
span course.course_topics
end
div do
label "Course Expectations:"
span course.course_expectations
end
div do
label "Time:"
span course.course_time
end
div do
label "Additional Resources:"
span course.course_additional_resources
end
end
panel "Course Overview" do
table_for course.units do
column "Unit name", :name
column(:videos) {|unit|
table_for unit.videos do
column :id
column :name
column :data
end
}
column(:quiz) { |unit|
if unit.quiz
table_for unit.quiz do
column :id
column :name
column :question
column :answer1
column :answer2
column :answer3
column :answer4
column :correct
end
else
'No Quiz'
end
}
end
end
active_admin_comments
end
sidebar "Course Units", only: :show do
# sidebar "Course Details", only: :show do
# attributes_table_for course do
# row :title
# row :author
# end
table_for Unit.joins(:course).where(:course_id => course.id) do |t|
t.column("Title") { |unit| unit.name }
end
end
form do |f|
tabs do
tab 'Basic Info' do
f.inputs "Course details" do
f.input :title
f.input :author
f.input :course_topics
f.input :course_expectations
f.input :course_time
f.input :course_additional_resources
end
end
tab 'Content' do
f.inputs "Units" do
f.has_many :units, heading: false, allow_destroy: true do |unit|
unit.input :name
unit.has_many :videos, heading: false, allow_destroy: true do |video|
video.input :name
video.input :data, label: 'Url', :as => :string, input_html: { class: 'video-tab' }
end
unit.has_one :quiz, heading: false, allow_destroy: true do |quiz|
quiz.has_many :questions, heading: false, allow_destroy: true do |q|
q.input :question, input_html: { class: 'quiz-tab' }
q.input :answer1, input_html: { class: 'quiz-tab' }
q.input :answer2, input_html: { class: 'quiz-tab' }
q.input :answer3, input_html: { class: 'quiz-tab' }
q.input :answer4, input_html: { class: 'quiz-tab' }
q.input :correct, input_html: { class: 'quiz-tab' }, as: :select, collection: [1,2,3,4]
end
end
end
end
end
end
f.actions
end
end