Ruby on rails 无效的单表继承类型:
我犯了这个错误,我不知道该怎么办。上面说Ruby on rails 无效的单表继承类型:,ruby-on-rails,Ruby On Rails,我犯了这个错误,我不知道该怎么办。上面说 Invalid single-table inheritance type: 1 is not a subclass of Game 它说这个错误发生在games_controller.rb中的9处 i、 e 我不明白发生了什么,我应该在这里写些什么来说明这个错误的原因。我复制并粘贴game.rb、user.rb、application\u controller.rb和games\u controller.rb 游戏。rb是 class Game &l
Invalid single-table inheritance type: 1 is not a subclass of Game
它说这个错误发生在games_controller.rb中的9处
i、 e
我不明白发生了什么,我应该在这里写些什么来说明这个错误的原因。我复制并粘贴game.rb、user.rb、application\u controller.rb和games\u controller.rb
游戏。rb是
class Game < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
end
因此,要回答这个问题,在迁移中不能有名为
type
的列名。我相信有很多方法可以解决这个问题,但最好的做法是将您的专栏重命名为其他内容。我以前见过这个错误。你的情妇有些事搞砸了。您可以发布包含游戏表单的视图吗?您还可以发布提交新游戏时在控制台中收到的请求吗?请使用新代码编辑您的帖子。那么,你没有游戏的表格?您是如何提交创建请求的?我可以在一条注释中开始新行吗?请参阅,您不能将类型
用作列名。
class Game < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
end
class User < ActiveRecord::Base
has_many :created_games, class_name: 'Game', foreign_key: :owner_id
def self.find_or_create_from_auth_hash(auth_hash)
provider = auth_hash[:provider]
uid = auth_hash[:uid]
nickname = auth_hash[:info][:nickname]
image_url = auth_hash[:info][:image]
User.find_or_create_by(provider: provider, uid: uid) do |user|
user.nickname = nickname
user.image_url = image_url
end
end
end
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :logged_in?
private
def current_user
return unless session[:user_id]
@current_user ||= User.find(session[:user_id])
end
def logged_in?
!!session[:user_id]
end
def authenticate
return if logged_in?
redirect_to root_path, alert:'Please log in.'
end
def created_by?(user)
return false unless user
owner_id == user.id
end
end
class GamesController < ApplicationController
before_action :authenticate
def new
@game = current_user.created_games.build
end
def create
@game = current_user.created_games.build(game_params)
if @game.save
redirect_to @game, notice: 'You created game.'
else
render :new
end
end
private
def game_params
params.require(:game).permit(
:name, :content, :type
)
end
end
<% now = Time.zone.now %>
<div class="page-header">
<h1>Create Game</h1>
</div>
<%= form_for(@game, class: 'form-horizontal', role:'form') do |f| %>
<% if @game.errors.any? %>
<div class="alert alert-danger">
<ul>
<% @game.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :type %>
<div>
<%= f.select :type, options_for_select: [['basic', 1]], class: 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :content %>
<%= f.text_area :content, class: 'form-control', row: 10 %>
</div>
<%= f.submit 'Create', class: 'btn btn-default', data: { disable_with: 'Creating...' } %>
<% end %>
Started POST "/games" for 127.0.0.1 at 2015-01-29 01:50:29 +0900
Processing by GamesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2W57cDKT2552Xgnh2MLi17uQpcrqDkmxhQJQa1qNuJNsZb0R10l/AU37y+KO9DysCp56aVTFBE/MqRoimMnjYQ==", "game"=>{"name"=>"this is name", "type"=>"1", "content"=>"this is content"}, "commit"=>"Create"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Completed 500 Internal Server Error in 32ms
ActiveRecord::SubclassNotFound - Invalid single-table inheritance type: 1 is not a subclass of Game: