I fixed this by adding @minors = Minor.all in the pins controller in the new method. And by replacing <%= collection_check_boxes(:minor_ids, :minor_ids, Minor.all, :id, :name) %>
with <%= f.collection_check_boxes(:minor_ids, @minors, :id,:name)%>
Has many to many checkboxes won't save Rails 4
-
19-07-2023 - |
Vra
I've tried making my has and belongs to many relation to work, following the Rails guides and everything I could find online, but they just wont show up.. When I save my form its only INSERTS into the Pin model, it doesn't insert any relations. If I add the relation manually via terminal like so:
p = Pin.new
p.minors = [1,2]
p.save
It works, but my form which looks like this (iam using rails 4, so collections yay) it doesn't save the relations (it saves the pin just fine)
new.html.erb
<%= collection_check_boxes(:minor_ids, :minor_ids, Minor.all, :id, :name) %>
<%= collection_check_boxes(:competence_ids, :competence_ids, Competence.all, :id, :name) %>
pinscontroller.rb
class PinsController < ApplicationController
def create
@pin = Pin.new(pin_params)
@pin.user_id = current_user.id
if @pin.save
redirect_to pin_path(@pin)
end
end
def new
@pin = Pin.new()
end
def show
@pin = Pin.find(params[:id])
end
private
def pin_params
params.require(:pin).permit(:title, :name, :url, { :minor_ids => [] }, { :competence_ids => [] },)
end
end
and my model Pin.rb
class Pin < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :minors
has_and_belongs_to_many :competences
end
Minor.rb
class Minor < ActiveRecord::Base
has_and_belongs_to_many :pins
end
Competence.rb
class Competence < ActiveRecord::Base
has_and_belongs_to_many :pins
end
pin/show.html.erb
This is the way I try to check if the relations came through. They all come back empty and 0, except for the ones I did through console.
<%= @pin.minors.count.inspect %>
<%= @pin.competences.count.inspect %>
<% if !@pin.minors.empty? %>
<%= @pin.minors.each do |minor| %>
<%= minor.name %>
<% end %>
<% end %>
<% if !@pin.competences.empty? %>
<%= @pin.competences.each do |comp| %>
<%= comp.name %>
<% end %>
<% end %>
Schema.rb
create_table "competences", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "pin_id"
end
create_table "competences_pins", force: true do |t|
t.integer "pin_id"
t.integer "competence_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "minors", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "pin_id"
end
create_table "minors_pins", force: true do |t|
t.integer "pin_id"
t.integer "minor_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "pins", force: true do |t|
t.string "title"
t.string "url"
t.string "image"
t.string "username"
t.integer "views"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
So, am I missing a step? So far i've tried everything but the minors_pins and the competences_pins tables remain empty, all the help and insight I can get!
Oplossing