简体   繁体   English

Rails:在不同模型中没有直接关联的情况下为@score创建表单

[英]Rails: Create form for @score while in different model no direct associations

I want to create a multiple form for editing scores from a different model. 我想创建多种形式来编辑来自不同模型的分数。

The main model is a Formrule model that consists of a habtm association with a Scoretype model and has a habtm association with a Room model. 主要模型是Formrule模型,该模型由habtm关联和Scoretype模型组成,并且habtm关联到Room模型。

Both models are used to query a Scores model resulting in a @scores instance. 这两个模型都用于查询产生@scores实例的Scores模型。 It is for this instance I want to create a form, but the problem is that no field_for are being created. 为此,我要创建一个表单,但是问题是没有创建field_for。 I know that the @scores is populated correctly, but the form does not show up. 我知道@scores已正确填充,但表单未显示。

This is the form as I have it now 这是我现在拥有的表格

<%= form_tag '/scores/update_scores' do %>
  <table>
    <tr>...</tr>
      <% for score in @scores %>
        <% fields_for :scores, score do |score| %>
           <tr>
              <td>
               <%= score.hidden_field(:form_id) %>
               <%= score.hidden_field(:team_id) %>
               <%= score.hidden_field(:scoretype_id) %>
            </td>
            <td>
              <%= score.number_field :scorevalue %>
            </td>
         </tr>
      <% end %>
    <% end %>
  </table>
  <%= submit_tag 'Update' %>
<% end %>

And these are the Models: Formrule 这些是模型:Formrule

class Formrule < ActiveRecord::Base
  belongs_to :form
  has_and_belongs_to_many :scoretypes
  has_and_belongs_to_many :rooms
  has_many :teams, :through => :rooms
end

Scoretype 得分类型

class Scoretype < ActiveRecord::Base
  has_many :scores
  has_and_belongs_to_many :formrules
end

Room 房间

class Room < ActiveRecord::Base
  has_many :teams
  has_and_belongs_to_many :formrules
end

Team 球队

class Team < ActiveRecord::Base
  has_many :scores
  belongs_to :room
  belongs_to :group
end

Score 得分

class Score < ActiveRecord::Base
  belongs_to :form
  belongs_to :team
  belongs_to :scoretype
  validates_uniqueness_of :id, :scope => [:team, :scoretype]
end

And finally, the used controller (Formrule) 最后是二手的控制器(Formrule)

  def show
    @formrule = Formrule.find(params[:id])
    @scoretypes = @formrule.scoretypes.all.collect
    @rooms = @formrule.rooms.all.collect
    @teams = Team.find(:all, :conditions => {:room_id => @rooms})
    @scores = Score.order("team_id").all(:conditions => {:scoretype_id => @scoretypes, :team_id => @teams})
...

end 结束

Why is the form not showing up? 为什么表格不显示? any suggestions? 有什么建议么?

Thank you all in advance! 谢谢大家!

尝试使用<%= fields_for ... %>代替<% fields_for ...%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM