Вопрос

I have a few models Score ScoreAuthority ScoreColor and ScoreType. Score has_one authority, color, and type. Authority, color, and type each belong_to score. I am having trouble creating the form for Score.

I want users to create this relationship from Scores and everyone seems to handle only the reverse scenario creating from authorities, colors, types forms. What I mean specifically is for example:

Authorities
 - foo
 - bar

Colors
 - red
 - blue

I want the use to go into Score, chose to create a new score and then select foo from the drop down list, blue from the colors list, enter a score and submit. Authorities, colors and types will not change much and it would make any sense for the user to select Score from each of those.

When I currently do that I get the following error:

ScoreAuthority(#2248480380) expected, got String(#2155957040) 

score/_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 tables all have score_id as a column. Each of these models also has belongs_to :score.

The score model

has_one :score_authority
has_one :score_type
has_one :score_color

Not sure what I am doing wrong here, any help would be appreciated. Thanks!

Это было полезно?

Решение

you should use :score_authority_id instead of :score_authority in form

<%= 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'} %>

see this How do I create the view for the has_one association? for more detail

or you can try

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 %>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top