NoMethodError (未定义的方法 'route=' for #<ActiveRecord:0x00007febf2e806f8>



我在这里需要一些帮助。我有一个属于一个帐户的社区模型。我使用设计进行身份验证的东西。现在的问题是,当我尝试提交/创建社区时,我收到此错误"未定义的方法'account_id=',用于 #Community:0x00007febf2e806f8你的意思是?帐户='

控制器

def create
@community = Community.new comunity_values
@community.account_id = current_account.id
if @community.save
redirect_to community_path
else 
render :new
end
end

private
def comunity_values
params.require(:community).permit(:name, :url,:rules)
end
end

class Community <ApplicationRecord
belongs_to :account
validates_presence_of :url, :name , :rules
end

迁移

class CreateCommunities < ActiveRecord::Migration[6.0]
def change
create_table :communities do |t|
t.references :account
t.string :name
t.string :url
t.text :rules
t.string :total_members
t.timestamps

end
end
end

在模型帐户中,您还必须引用社区模型。像这样:

has_many :community

其他人认为你必须知道的是,当你使用关系创建时,你不需要指定id,你可以以更易读的方式使用:

@community.account = current_account
@community.save

而且我认为你正在颠倒事情的顺序,因为一个帐户有很多社区,所以......您可以按如下方式执行此操作:

current_account.community.create!(comunity_values)

希望这有帮助

相关内容

最新更新