2

所以我有一个代表这种情况的模型:

  • 学生参加比赛。
  • 比赛衡量某些技能。
  • 在每场比赛中,每个学生都会获得所测量的每项技能的分数。

这是我的模型:

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

在我的比赛编辑视图中,我正在尝试使用 formtastic 创建一个表格,该表格将显示所有比赛参与者,并允许用户为比赛中的每个技能添加分数,如下所示。

但我收到一个错误(student_id 无效符号)。如何更新学生成绩?

<%= 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 %>
4

1 回答 1

0

尝试这个。添加 hidden 因为 e 认为您不希望 id 显示

<%= scores_form.input :student_id, :as => :hidden, :value => student.id %>
<%= scores_form.input :skill_id, :as => :hidden, :value => skill.id %>
于 2012-04-29T01:41:12.350 回答