Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails-没有方法错误_Ruby On Rails_Ruby_Nomethoderror - Fatal编程技术网

Ruby on rails RubyonRails-没有方法错误

Ruby on rails RubyonRails-没有方法错误,ruby-on-rails,ruby,nomethoderror,Ruby On Rails,Ruby,Nomethoderror,我正在完成这门airbnb克隆课程(),但为了完成我自己的项目,我做了一些改变;教育营的市场。因此,我添加了一个附加模型。它现在有用户>列表>课程 自从添加此课程以来,我在运行服务器并转到本地主机时一直收到以下错误:3000/courses/new我尝试搜索stackoverflow上的问题,但我没有足够的经验来描述并因此找到问题,如果有任何想法,我将不胜感激 错误消息 undefined method `curriculum_type' for #<Listing:0x007fb776a

我正在完成这门airbnb克隆课程(),但为了完成我自己的项目,我做了一些改变;教育营的市场。因此,我添加了一个附加模型。它现在有用户>列表>课程

自从添加此课程以来,我在运行服务器并转到本地主机时一直收到以下错误:3000/courses/new我尝试搜索stackoverflow上的问题,但我没有足够的经验来描述并因此找到问题,如果有任何想法,我将不胜感激

错误消息

undefined method `curriculum_type' for #<Listing:0x007fb776ac0f50>
Extracted source (around line #15):


            <div class="form-group">
              <label>Course Type</label>
              <%= f.select :curriculum_type, [["English Language", "English Language"], ["Culture", "Culture"], ["Sports", "Sports"], ["Tech/Science", "Tech/Science"], ["Adventure", "Adventure"], ["Mixture", "Mixture"]],
                            id: "type", prompt: "Select...", class: "form-control" %>
            </div>
          </div>
未定义的“课程类型”方法#
提取的源(第15行附近):
课程类型
型号

class User < ApplicationRecord
  has_many :listings
  has_many :courses, :through => :listings
end

class Listing < ApplicationRecord
  belongs_to :user
  has_many :courses
end

class Course < ApplicationRecord
  belongs_to :listing
end
class用户:列表
结束
类列表<应用程序记录
属于:用户
你有很多课程吗
结束
班级课程<申请记录
属于:列表
结束
课程控制员

class CoursesController < ApplicationController
before_action :set_course, except: [:index, :new, :create]
before_action :authenticate_user!, except: [:show]

  def index
    @courses = current_user.listings.courses
  end

  def new
    @course = current_user.listings.build
  end

  def create
    @course = listings.build(course_params)
    if @course.save
      redirect_to listing_course_path(@course), notice: "Saved..."
    else
      render :new, notice: "Something went wrong..."
    end
  end

  def show

  def listing
  end

  def pricing
  end

  def description
  end

  def photo_upload
  end

  def amenities
  end

  def location
  end

  def update
    if @course.update(course_params)
      flash[:notice] = "Saved..."
    else
      flash[:notice] = "Something went wrong..."
    end
    redirect_back(fallback_location: request.referer)
  end

  private

  def set_course
    @course = Course.find(params[:id])
  end

  def room_params
    params.require(:course).permit(:name, :course_type, :summary, :address, :course_places, :start_date, :finish_date, :price)
  end

end
end
class CoursesController
Courses new.html.erb

<div class="panel panel-default">
  <div class="panel-heading">
    Create your course listing
  </div>
  <div class="panel-body">
    <div class="devise-container">

      <%= form_for @course do |f| %>
        <div class="row">
          </div>

          <div class="col_md_4 select">
            <div class="form-group">
              <label>Course Type</label>
              <%= f.select :curriculum_type, [["English Language", "English Language"], ["Culture", "Culture"], ["Sports", "Sports"], ["Tech/Science", "Tech/Science"], ["Adventure", "Adventure"], ["Mixture", "Mixture"]],
                            id: "type", prompt: "Select...", class: "form-control" %>
            </div>
          </div>

          <div class="col_md_4 select">
            <div class="form-group">
              <label>Places</label>
              <%= f.select :course_places, [["1", 1], ["2", 2], ["3", 3], ["4", 4], ["5", 5], ["6", 6], ["7", 7]],
                            id: "places", prompt: "Select...", class: "form-control" %> -->
            </div>
          </div>

        </div>

        <div><%= f.submit "Create My Course", class: "btn btn-primary-green" %></div>
      <% end %>

    </div>
  </div>
</div>

创建课程列表
课程类型
地点
-->


在Rails控制台中,我可以创建所有模型,但当我启动服务器时,它似乎无法识别课程模型

您的控制器正在构建一个
列表
对象,但您的
课程类型
属性是
课程
的属性。如果是这种情况,您需要修改控制器以生成
课程
对象,或者将属性添加到
列表
您的控制器正在生成
列表
对象,但是
课程类型
属性是
课程
的属性。如果是这种情况,您需要修改控制器以生成
课程
对象,或者将属性添加到
列表

schema.rb文件中的内容?看起来您还没有为列表添加
课程类型
属性。@colincr它在那里:create_table“courses”,force::cascade do | t | t.string“name”t.string“courseum_type”您运行过迁移吗?schema.rb文件中有什么?看起来您还没有为列表添加
课程类型
属性。@colincr它在那里:create_table“courses”,force::cascade do | t | t.string“name”t.string“courseum_type”您运行迁移了吗?谢谢!那么我的新方法应该是这样的吗?def new@course=current_user.listings.courses.build end?看起来正确;您的课程是否正确保存?不幸的是,这个新方法不起作用,新错误:“未定义的方法`课程'for#”恐怕我不确定它是否正确保存,如果这是您的意思,它在rails控制台中工作?是的,这就是我的意思。您的
课程
已保存,没有回滚,并且已由ActiveRecord提供ID。谢谢!那么我的新方法应该是这样的吗?def new@course=current_user.listings.courses.build end?看起来正确;您的课程是否正确保存?不幸的是,这个新方法不起作用,新错误:“未定义的方法`课程'for#”恐怕我不确定它是否正确保存,如果这是您的意思,它在rails控制台中工作?是的,这就是我的意思。您的
课程
保存时没有回滚,并且ActiveRecord为您提供了一个ID。