Ruby on Rails - 没有方法错误

时间:2017-10-22 17:39:00

标签: ruby-on-rails ruby nomethoderror

我正在完成这个airbnb克隆课程(https://code4startup.com/projects/build-airbnb-with-ruby-on-rails-level-1),但为了完成我自己的项目已经转移了一些;教育营地的市场。因此我添加了一个额外的模型。它现在有用户>列表>课程。

由于添加了这个课程,我在运行服务器时继续收到以下错误并转到localhost: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>

模型

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 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

课程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控制台中,我可以创建所有模型,但在启动服务器时似乎无法识别课程模型

1 个答案:

答案 0 :(得分:2)

您的控制器正在构建Listing对象,但您的curriculum_type属性是Course的属性。如果是这种情况,您需要修改控制器以构建Course对象,或者将属性添加到Listing