Ruby on rails 4 rails4无法创建关系can';不写入未知属性
我有以下代码 db: 用户:Ruby on rails 4 rails4无法创建关系can';不写入未知属性,ruby-on-rails-4,strong-parameters,Ruby On Rails 4,Strong Parameters,我有以下代码 db: 用户: class用户
class用户
缔约方:
class Party < ActiveRecord::Base
belongs_to :host, class_name: 'User', foreign_key: 'host_id'
attr_accessor :user_id
end
class Party
缔约方/财务主任:
class PartiesController < ApplicationController
def create
@party = current_user.host_parties.create(party_params)
end
private
def party_params
params.require(:party).permit(:host_id, :guests, :description)
end
end
class PartiesController
由于某些原因,创建失败:无法写入未知属性“user\u id”
如何使rails将User.id视为主机id?在
中,在您的用户
模型中有许多关系,您需要指定外键
,因为默认值是用户id
以下方面应起作用:
class User < ActiveRecord::Base
has_many :host_parties, class_name: Party, foreign_key: :host_id
end
class用户
class PartiesController < ApplicationController
def create
@party = current_user.host_parties.create(party_params)
end
private
def party_params
params.require(:party).permit(:host_id, :guests, :description)
end
end
class User < ActiveRecord::Base
has_many :host_parties, class_name: Party, foreign_key: :host_id
end