So I have a model below that represents the situation:
- Students participate in Contests.
- Contests measure certain skills.
- For each contest, each student gets a score for each skill measured.
Here are my models:
class Student < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :name
has_and_belongs_to_many :skills
has_many :contest_participations
has_many :contests, :through => :contest_participations
has_many :contest_scores, :through => :contests
end
class Skill < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :students
end
class Contest < ActiveRecord::Base
attr_accessible :name, :contest_participations_attributes, :contest_scores_attributes
has_many :contest_participations
has_many :students, :through => :contest_participations
has_many :contest_skills
has_many :skills, :through => :contest_skills
has_many :contest_scores
accepts_nested_attributes_for :contest_participations, :contest_scores
end
class ContestParticipation < ActiveRecord::Base
attr_accessible :contest_id, :student_id
belongs_to :student
belongs_to :contest
end
class ContestScore < ActiveRecord::Base
attr_accessible :contest_id, :score, :skill_id, :student_id
has_and_belongs_to_many :contests
has_and_belongs_to_many :skills
has_and_belongs_to_many :student
end
In my contests edit view, I’m trying to create a form, with formtastic, that will show all contest participants, and allow the user to add a score for each skill in the contest as follows.
But I get an error (student_id invalid symbol). How can I update the student scores?
<%= semantic_form_for @contest do |f| %>
<%= f.input :name %>
<%= @contest.students.each do |student| %>
<%= @contest.skills.each do |skill| %>
<%= f.inputs :name => "Scores", :for => :contest_scores do |scores_form| %>
<%= scores_form.input :student_id => "student.id" %>
<%= scores_form.input :skill_id => "skill.id" %>
<%= scores_form.input :score, :label => "Score" %>
<% end %>
<br />
<% end %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :button %>
<% end %>
<% end %>
Try this. Added hidden because e think you don’t want ids to show up