我想添加/刪除記錄到我的主要表單對象Report
的關聯。我有我的multiselect下拉列表正確顯示,並正確地將ID傳遞給我的更新操作。但是,我收到以下消息。如何通過我的Rails窗體創建/刪除對象到關聯?
Couldn't find all ReportExposures with 'id': (157504, 148644, 152852) (found 0 results, but was looking for 3)
現在我知道,這些ID其實我ncaa_game_id
了,我就是想分配給ReportExposure
記錄,但我不能把ReportExposure id
,因爲它並沒有真正存在呢。我需要對錶單進行調整才能使其正常工作,或者需要在更新操作中添加一些代碼來處理這些問題?
Report.rb
class Report < ActiveRecord::Base
has_many :report_exposures
end
ReportExposure.rb
class ReportExposure < ActiveRecord::Base
belongs_to :ncaa_game
belongs_to :report
end
ReportsController.rb
def update
# "report_exposure_ids"=>["157504","148644","152852"] -- these ids are really the ncaa_game_ids I want to create new report_exposure objects with...
respond_to do |format|
if @report.update(report_params)
format.html
format.json
end
end
end
份_form.html.erb
<select id="report_report_exposure_ids" name="report[report_exposure_ids][]" class="multiselect-dropdownlist" multiple="multiple">
<% @exposures.each do |season| %>
<optgroup label="<%= season.first.season %> Season">
<% season.includes(:home_team, :away_team).order(game_date: :asc).each do |game| %>
<option value="<%= game.id %>"><%= game.full_description %></option>
<% end %>
</optgroup>
<% end %>
</select>
如果我要這樣做,我需要與連接表進行HABTM關係。我試圖用我描述的方式來處理這種關係,以防將來需要添加元數據。例如在「ReportExposure」記錄中添加評分或評論。 – daveomcd
我認爲你仍然可以兼得。你可以使用has_many_through關係,並讓你的表單按照我的建議工作。如果出於某種原因想要在表單中公開連接表,則需要爲所選的每個game_id創建一條新記錄。你的錯誤是你在說'name ='report [report_exposure_ids] []「',然後使用遊戲ID作爲選項中的id。 – Spencer