我正在通过Code4Startup airbnb课程(https://code4startup.com/projects/build-airbnb-with-ruby-on-rails-level-1),但我正在尝试根据自己的想法进行调整 - 这是我为当地大学开设的教育课程。我是ruby和stackoverflow的新手,我已经尝试寻找解决方案多年了,但我的问题是我不知道如何正确描述它,所以请解释它就像我5岁!
我收到的错误:
错误似乎表明我的课程模型没有正确构建,或者它以某种方式恢复到列表模型(也许是因为我的course_params不正确?)
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&gt; Listing&gt; 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
课程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 :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>
答案 0 :(得分:1)
type
是Rails中的保留字。您可以创建具有type
属性的模型,但在重命名type属性之前,您无法对此模型执行操作。
如果您尝试通过rails控制台创建新记录,则会看到如下消息:
$ rails console
[1] pry(main)> Course.new(name: 'first', type: 'some type')
ActiveRecord :: SubclassNotFound:单表继承机制 找不到子类:&#39;某种类型&#39;。提出此错误是因为 列&#39;类型&#39;保留用于存储类的情况 遗产。如果您不打算将此列重命名,请重命名 用于存储继承类或覆盖 Course.inheritance_column使用另一列来获取该信息。 从 /Users/***/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.4/lib/active_record/inheritance.rb:196:in `find in find_sti_class&#39;
如消息所示,您必须重命名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
只是我的一个不好的建议,你可以根据需要调整它。