0

オプションで rails has_many を使用していますが、ここで何か間違っているかどうかわかりません。プレーヤーにシーズンを作成してもらいたいのですが、プレーヤーがシーズンを作成しようとすると、作成したすべてのシーズンの選択メニューが年/新しい年に表示されます。これまでのところ、その部分はうまく機能していますが、プレーヤーがシーズンレールを保存しようとすると、それは保存されません. 自分の関連付けが正しいのか、それとも何か間違っているのかわかりません。これが機能しない理由はありますか?

エラー

No association found for name `season_year'. Has it been defined yet?

シーズン.rb

class season < ActiveRecord::Base
 belongs_to :player

 has_many :quarters
 has_many :years , :through => :quarters

 attr_accessible :season_year_attributes
 accepts_nested_attributes_for :season_year 
end

四半期.rb

class Quarter < ActiveRecord::Base
  belongs_to :player
  belongs_to :year
  belongs_to :season
end

年.rb

class Year < ActiveRecord::Base
  attr_accessible :season_year, :season_name

  has_many :quarters
  has_many :seasons, :through => :quarters 
end

player.rb

class player < ActiveRecord::Base
  has_many :seasons, :through => :quarters
  has_many :years, :through => :quarters
end

_season-form.html.erb

<%= form_for(@season) do |f| %>
  <div class="field">
    <%= f.label :season %>
    <%= f.text_field :season_name %>
  </div>

<%= f.fields_for :years do |year| %>
<%= select("season", "year_ids", Year.all.collect {|p| [ p.season_year, p.id ] }, { :include_blank => true }) %>
<% end %>

<div class="actions">
  <%= f.submit %>
</div>
<% end %>
4

2 に答える 2

2

あなたのモデルに基づいて、これを変更する必要があると思います:

accepts_nested_attributes_for :season_year 

これに:

accepts_nested_attributes_for :years

ネストされた属性を受け入れる場合、それはモデル用であり、モデルのプロパティではありません (season_year は、実際のモデルである Year のネストされた属性を受け入れるのに対して、年モデルの属性です)。

編集:

Season モデルでは、attr_accessible 式に year_ids を追加しました。

attr_accessible :season_year_attributes, :year_ids

また、年ごとの選択リストの出力が次のようになるように、季節の形式も変更しまし

<%= select("season", "year_ids", Year.all.collect {|p| [ p.title, p.id ] }, { :include_blank => true }) %>
于 2012-04-26T20:54:55.820 に答える
1

Seasonクラスにseason_yearがない、それが答えです。

これを協会として持つつもりでしたか?

于 2012-04-26T20:50:17.850 に答える