考虑这些简单的提醒&事件模型:
class Event < ActiveRecord::Base
has_many :reminders
end
class Reminder < ActiveRecord::Base
belongs_to :event
accepts_nested_attributes_for :event
end
现在,我已按以下方式构建reminders/new.html.haml
视图:
%h2 Reminders
= form_for(@reminder, html: {class: "form-horizontal"}) do |f|
= f.fields_for :event do |event|
= event.collection_select :id, Event.all, :id, :name
= f.text_field(:issue_date, class: "datepicker", data: {"date-format" =>"dd/mm/yyyy"}, 'date' => "#{Time.new.strftime("%d/%m/%Y")}")
= f.text_field(:renewal_date, label:"Due Date", class: "datepicker", data: {'date-format' => 'dd/mm/yyyy', 'date' => "#{Time.new.strftime("%d/%m/%Y")}"})
= f.submit
此视图不会引发任何错误。提醒减去<options>
标记。
View http://screencloud.net/img/screenshots/f990e124f356330a10839b00cd92f75a.png
我想要做的是在Event.name
中显示<options>
。那我在这里错过了什么?
[更新]
已更改为form_for
&amp;它生成以下html标记
<form method="post" id="new_reminder" class="form-horizontal" action="/reminders" accept-charset="UTF-8"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"><input type="hidden" value="Yu7Qj4SW+GAMXx9dEL/ohjsqryEkxUXXGy+SHpN97hc=" name="authenticity_token"></div>
<input type="text" size="30" name="reminder[issue_date]" id="reminder_issue_date" date="20/09/2012" data-date-format="dd/mm/yyyy" class="datepicker">
<input type="text" size="30" name="reminder[renewal_date]" label="Due Date" id="reminder_renewal_date" data-date="20/09/2012" data-date-format="dd/mm/yyyy" class="datepicker">
<input type="submit" value="Create Reminder" name="commit">
</form>
您可能会注意到<option>
标记由于某种原因而丢失。
答案 0 :(得分:0)
试试这个: collection_select(:event,:id,Event.all,:id,:name)
答案 1 :(得分:0)
我做错了,此解决方案不需要accepts_nested_attributes_for
。删除它&amp;问题解决了。