Trying to add data to the scrapbook_entries join table that has has_one: scrapbook and has_one: recipe.
: recipe and: notes already exist. I am trying to add them to link them to the scrapbook_entries table.
form_ to add to the scrapbook_entries table:
<%= 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
When I submit the form to the controller, I get an error message:
can't write unknown attribute `scrapbook_entry_id'
Can someone tell me what I am doing wrong?
Update:
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
ruby join ruby-on-rails controller model
Mikeholford
source share