has_one:scrapbookとhas_one:recipeというscrapbook_entriesの結合テーブルにデータを追加しようとしています。は未知の属性 `scrapbook_entry_id 'を書き込むことができません
:レシピ:スクラップブックは既にあります。私はそれらをscrapbook_entriesテーブルにリンクするために追加しようとしています。 scrapbook_entriesテーブルに追加
のform_for:
<%= 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 %>
scrapbook_entries_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
scrapbook.rb
has_many :recipes, through: :scrapbook_entries
has_many :scrapbook_entries
recipe.rb
has_many :scrapbooks, through: :scrapbook_entries
私はエラーを取得していますコントローラにフォームを提出するには scrapbook_entry.rb
has_one :recipe
has_one :scrapbook
:
can't write unknown attribute `scrapbook_entry_id'
誰もが私が間違っているのを教えすることはできますか?
更新:
schema.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
あなたが作成アクションで受け取っているパラメータを開発ログに貼り付けることができます –