我需要帮助解决以下问题。我一直在寻找类似的问题,但我仍然无法找到问题的解决方案。我目前遇到这个问题。
课程#edit
中的NoMethodError#Class的未定义方法`model_name':0xb5068474
错误似乎与这行代码有关。
<%= simple_fields_for @lesson do |f| %>
<%= f.input :lesson_name %>
<%end%>
课程数据库是课程数据库的链接,其中课程有很多课程,课程属于课程。我认为问题可能是由于我的控制器代码。我相对较新,不太确定如何解决这个问题。
我可以创建页面来创建该关系,但是没有其他内容保存在数据库中,但是课程和课程的ID。但是,当我进入编辑页面时,会弹出此错误。
courses_controller.rb
class CoursesController < ApplicationController
before_action :set_course, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, :except => [:show,:index]
# GET /courses
# GET /courses.json
def index
@courses = Course.all
end
# GET /courses/1
# GET /courses/1.json
def show
end
# GET /courses/new
def new
@course = Course.new
@lesson = @course.lessons.build
end
# GET /courses/1/edit
def edit
@course = Course.find(params[:id])
@lesson = @course.lessons
end
# POST /courses
# POST /courses.json
def create
@course = Course.new(course_params)
@course.lessons.new(lesson_params)
respond_to do |format|
if @course.save
format.html { redirect_to @course, notice: 'Course was successfully created.' }
format.json { render action: 'show', status: :created, location: @course }
else
format.html { render action: 'new' }
format.json { render json: @course.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /courses/1
# PATCH/PUT /courses/1.json
def update
respond_to do |format|
if @course.update(course_params)
@course.lesson.update_attributes(lesson_params)
@course.staff_ids=params[:course][:staff_ids]
format.html { redirect_to @course, notice: 'Course was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @course.errors, status: :unprocessable_entity }
end
end
end
# DELETE /courses/1
# DELETE /courses/1.json
def destroy
@course.destroy
respond_to do |format|
format.html { redirect_to courses_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
@course = Course.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:course_code, :course_name, :year_of_study, :discipline, :Acad_unit, :cohort_size, :remark)
end
def lesson_params
params.require(:lesson).permit(:lesson_type, :lesson_name, :num_lesson, :frequency)
end
end
lesson.rb
class Lesson < ActiveRecord::Base
belongs_to :course
end
course.rb
class Course < ActiveRecord::Base
validates_presence_of :course_code, :course_name
validates_uniqueness_of :course_code, :course_name
validates :year_of_study, :Acad_unit, :cohort_size, :numericality => { :greater_than => 0}
has_and_belongs_to_many :staffs, join_table: :scheduleCourse
has_many :lessons, dependent: :destroy
end
请给我一些建议。提前致谢
答案 0 :(得分:0)
你得到的错误:
undefined method `model_name` for #Class:0xb5068474
表示simple_fields_for
方法正在执行类似@lesson.class.model_name
的操作,当您传递的对象(@lesson
)是ActiveRecord :: Base的实例时,这是正常的,但在{{1}中你定义edit
@lesson
作为一种关系,它不是一个@lesson = @course.lessons
实例,它是用于存储关系的内部ActiveRecord对象。例如,如果你打电话给Lesson
:
first
这应该有效。或者您可以在视图中迭代课程集合,为每个课程创建表单,这取决于您想要做什么。