2014-02-27 47 views
0

我需要以下问题的帮助。我一直在寻找类似的问题,但仍然无法找到解决问题的方法。我目前有这个问题。课程中的NoMethodError#编辑未定义的方法`model_name'

NoMethodError在课程#编辑

未定义的方法`MODEL_NAME”的#Class:0xb5068474

错误似乎与这行代码。

<%= simple_fields_for @lesson do |f| %> 
       <%= f.input :lesson_name %> 
<%end%> 

课程数据库链接到课程数据库,当然has_many课程和课程belongs_to课程。我认为这个问题可能是由于我的控制器代码。我相对较新,对于如何解决这个问题还不太确定。

我能够去创建页面来创建该关系,但没有别的东西被保存在数据库中,但是当然和课程的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

请提供课程和课程模型代码 – lobanovadik

+0

添加的课程和课程模型代码 – Alvin2187

回答

0

,你得到的错误:

undefined method `model_name` for #Class:0xb5068474 

就是说simple_fields_for方法做这样的事情@lesson.class.model_name,当对象,你传递这是确定(@lesson)是的ActiveRecord :: Base的实例,但在edit动作定义@lesson

@lesson = @course.lessons 

作为关系,所以它不是一个Lesson例如,它是用于存储R内部的ActiveRecord对象elations。例如,如果您拨打first就可以了:

@lesson = @course.lessons.first 

这应该有效。或者你可以遍历视图中的每一堂课来创建表单,这取决于你想要做什么。

+0

我想在创建课程时随时创建几个课程。我尝试了chaining the @ lesson = @ course.lesson而不是课程,它不起作用。我尝试使用@ lesson = @ course.lessons.first,但它也不起作用:x – Alvin2187

+0

这可能是因为你没有吸取教训。尝试在你的控制器中使用'@ course.lessons.build',并在'<%@ course.lessons.each do | lesson | %>'。查看嵌套表单手册,这就是你想要做的https://github.com/plataformatec/simple_form/wiki/Nested-Models – lobanovadik

+0

我创建了3个字段供用户在创建课程时输入3个不同的课程。每个使用<%= simple_fields_for @lesson do | f | %> <%= f.input:lesson_name%> <%end%>。这仍然使用<%@ course.lessons.each do | lesson | %>。当然创建是一种形式,所以它被认为是嵌套形式? – Alvin2187

相关问题