gpt4 book ai didi

ruby-on-rails - 无法写入未知属性 `scrapbook_entry_id'

转载 作者:数据小太阳 更新时间:2023-10-29 06:52:28 26 4
gpt4 key购买 nike

正在尝试将数据添加到 scrapbook_entries 的连接表中,其中 has_one :scrapbook 和 has_one :recipe。

:recipe 和 :scrapbook 已经存在。我正在尝试添加它们以将它们与 scrapbook_entries 表链接起来。

form_for 添加到 scrapbook_entries 表:

<%= form_for(@scrapbook_entry, :url => scrapbook_entries_path(params[:id])) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%=f.select(:scrapbook_id, current_user.scrapbooks.collect {|p| [ p.name, p.id ] }, {prompt: 'Select Scrapbook...'})%>
<%= f.hidden_field :recipe_id, :value => @recipe.id %>
</div>
<%= f.submit "Save", class: "btn btn-large btn-primary" %>
<% end %>

剪贴簿_条目_ Controller :

def create
@recipe = Recipe.find(params[:scrapbook_entry][:recipe_id])
@scrapbook = current_user.scrapbooks.find(params[:scrapbook_entry][:scrapbook_id])

@entry = @scrapbook.scrapbook_entries.build(scrapbook: @scrapbook)
if @entry.save
flash[:success] = "Added '#{@recipe.name}' to scrapbook '#{@scrapbook.name}'"
else
flash[:error] = "Could not add to scrapbook"
end
redirect_to @recipe
end

剪贴簿.rb

has_many :recipes, through: :scrapbook_entries
has_many :scrapbook_entries

食谱.rb

has_many :scrapbooks, through: :scrapbook_entries

剪贴簿_entry.rb

has_one :recipe
has_one :scrapbook

在将表单提交给 Controller 时出现错误:

can't write unknown attribute `scrapbook_entry_id'

谁能告诉我我做错了什么?

更新:

架构.rb

 create_table "scrapbook_entries", force: true do |t|
t.integer "scrapbook_id"
t.integer "recipe_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end

最佳答案

你的 scrapbook_entr.rb 应该包含

belongs_to :recipe
belongs_to :scrapbook

而且还没有!

当您的表包含另一个表的外键时,您总是使用 belongs_to,在这种情况下确实如此!

关于ruby-on-rails - 无法写入未知属性 `scrapbook_entry_id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com