NoMethodError - Ruby on Rails



我正在学习Code4Startup airbnb课程(https://code4startup.com/projects/build-airbnb-with-ruby-on-rails-level-1)但我正在努力适应我自己的想法——为我当地的大学开设一个教育课程的airbnb。我是ruby和stackoverflow的新手,我已经尝试寻找解决方案很长时间了,但我的问题是我不知道如何正确描述它,所以请像我5岁一样解释它!

我收到的错误:

这个错误似乎表明我的课程模型没有正确构建,或者它正在以某种方式恢复到Listing模型(也许是因为我的课程图不正确?)

NoMethodError in Courses#new
Showing /Users/Jack_R/code/rails/planet_study/app/views/courses/new.html.erb where line #22 raised:
undefined method `type' for #<Listing:0x007fc25da72ea8>
Extracted source (around line #22):
20
21
22
<div class="form-group">
<label>Course Type</label>
<%= f.select :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>

我的型号

我有一个User模型、Listing模型和Course模型(User>Listing>Course):

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, :type, :summary, :address, :places, :start_date, :finish_date, :price)
end
end
end

新课程.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 :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 :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>

type是Rails中的一个保留字。可以创建具有type属性的模型,但在重命名类型属性之前,不能对此模型执行操作。

如果你试图通过rails控制台创建一个新记录,你会看到一条消息,比如:

$ rails console
[1] pry(main)> Course.new(name: 'first', type: 'some type')

ActiveRecord::子类NotFound:单表继承机制未能定位子类:"some-type"。引发此错误的原因是列"type"是为在发生以下情况时存储类而保留的遗产如果您不打算重命名此列,请将其重命名用于存储继承类或覆盖Course.inheritance_column以使用另一列作为该信息。从…起/用户/***.rbenv/versions/2.4.1/lib/rube/gems/2.4.0/gems/activerecord-5.1.4/lib/active_record/heritance.rb:196:in`find_sti_class'中的救援

正如消息所说,您必须重命名type属性才能使用它,为此只需运行迁移来更改此名称并编辑创建的文件,如:

$ rails generate migration rename_type_to_type_of

在生成的文件中,使用rename_colum方法,首先指定模型,然后指定旧属性名称,然后指定新属性名称,如:

class RenameTypeToTypeOf < ActiveRecord::Migration[5.1]
def change
rename_column :courses, :type, :type_of
end
end

之后,您可以运行rails db:migrate。注意,type_of只是我的一个糟糕的建议,你可以根据自己的意愿进行调整。

最新更新