I have two models, articles and goals connected through a third mentorships association (has_many through). I'm trying to set it up so the mentorship record is created through a checkbox in a form, which displays all goals via a checkbox. Right now the form submits and a mentorship is created with the article id, but the goal id is always nil. What am I doing wrong?
mentorships/_form.html.erb
<div class="log-form">
<%= form_for(@mentorship) do |f| %>
<%= label_tag 'Mark This As Helpful' %><br />
<% current_user.goals.each do |goal| %>
<%= check_box_tag "article[goal_ids][]", goal.id %>
<%= goal.title %><br />
<% end %>
<div><%= hidden_field_tag :article_id, @article.id %></div>
<p><%= f.submit %></p>
<% end %>
</div>
article.rb
class Article < ApplicationRecord
belongs_to :user, required: true
has_many :mentorships
has_many :goals, through: :mentorships
goal.rb
class Goal < ApplicationRecord
has_many :mentorships
has_many :articles, through: :mentorships
mentorship.rb
class Mentorship < ApplicationRecord
belongs_to :article
belongs_to :goal
end
mentorships_controller.rb
class MentorshipsController < ApplicationController
def new
@mentorship = Mentorship.build
end
def create
@mentorship = Mentorship.new(mentorship_params)
@mentorship.save
end
private
def mentorship_params
params.permit(:article_id, :goal_ids => [])
end
end
Aucun commentaire:
Enregistrer un commentaire