如何通过我的 Rails 表单 create/remove 反对协会?
How do I create/remove objects to an association through my Rails form?
我想 add/remove 从我的主表单对象 Report
记录到关联。我的多选下拉列表正确显示并将 ID 正确传递给我的更新操作。但是,我收到了以下消息。
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>
将报告更改为:
class Report < ActiveRecord::Base
has_many :report_exposures
has_many :ncaa_games
accepts_nested_attributes_for :ncaa_games
end
并在此处阅读有关执行嵌套表单的信息:
http://guides.rubyonrails.org/form_helpers.html#nested-forms
有很多关于 nested_forms 和 has_many through 关系的人提问的例子。查看这些以获取指导:
Rails has_many :through nested form
不要尝试操纵连接对象 (ReportExposure),除非您实际上是在处理向该对象添加元数据的问题。这就是 ORM 的用途。
我想 add/remove 从我的主表单对象 Report
记录到关联。我的多选下拉列表正确显示并将 ID 正确传递给我的更新操作。但是,我收到了以下消息。
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>
将报告更改为:
class Report < ActiveRecord::Base
has_many :report_exposures
has_many :ncaa_games
accepts_nested_attributes_for :ncaa_games
end
并在此处阅读有关执行嵌套表单的信息:
http://guides.rubyonrails.org/form_helpers.html#nested-forms
有很多关于 nested_forms 和 has_many through 关系的人提问的例子。查看这些以获取指导:
Rails has_many :through nested form
不要尝试操纵连接对象 (ReportExposure),除非您实际上是在处理向该对象添加元数据的问题。这就是 ORM 的用途。