我有几个模型Score
ScoreAuthority
ScoreColor
和ScoreType
。得分has_one
权限,颜色和类型。权限,颜色和每个belong_to
分数的类型。我在创建Score的表单时遇到问题。
我希望用户从Scores创建这种关系,每个人似乎只处理从权限,颜色,类型表单创建的反向场景。我的具体含义是例如:
Authorities
- foo
- bar
Colors
- red
- blue
我希望使用进入分数,选择创建新分数,然后从下拉列表中选择foo
,从颜色列表中选择blue
,输入分数并提交。权威,颜色和类型不会有太大变化,用户从每个中选择分数都是有意义的。
当我这样做时,我收到以下错误:
ScoreAuthority(#2248480380) expected, got String(#2155957040)
得分/ _form.html.erb
<%= form_for @score, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :product_id, "Products", :class => 'control-label' %>
<div class="controls">
<%= f.collection_select(:product_id, Product.order(:name), :id, :name) %>
</div>
</div>
<div class="control-group">
<%= f.label :score_authority, "Score Authority", :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :score_authority, ScoreAuthority.order(:name), :id, :name, {}, {:class=>'chosen'} %>
</div>
</div>
<div class="control-group">
<%= f.label :score_type, "Score Types", :class => 'control-label' %>
<div class="controls">
<%#= f.collection_select(:score_type, ScoreType.order(:name), :id, :name) %>
<%= f.collection_select :score_type, ScoreType.order(:name), :id, :name, {}, {:class=>'chosen'} %>
</div>
</div>
<div class="control-group">
<%= f.label :score_color, "Score Types", :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :score_color, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>
</div>
</div>
<div class="control-group">
<%= f.label :notation, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :notation, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :value, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :value, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
scores_path, :class => 'btn' %>
</div>
<% end %>
score_authorities,score_colors,score_types表都有score_id作为列。这些模型中的每一个都有belongs_to :score
。
得分模型
has_one :score_authority
has_one :score_type
has_one :score_color
不确定我在这里做错了什么,任何帮助都将不胜感激。谢谢!
答案 0 :(得分:0)
您应该使用:score_authority_id而不是:表格中的score_authority
<%= f.collection_select :score_authority_id, ScoreAuthority.order(:name), :id, :name, {}, {:class=>'chosen'} %>
<%= f.collection_select :score_color_id, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>
<%= f.collection_select :score_color_id, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>
请参阅此How do I create the view for the has_one association?了解更多详情
或者你可以尝试
class Score
has_one :score_authority
accepts_nested_attributes_for :score_authority
end
<%= form_for @score ... do |f| %>
...
<%= f.fields_for :score_authority do |b| %>
<%= b.collection_select :id, ScoreAuthority.all, :id, :name %>
...
<% end %>
...
<% end %>