0

考虑这些简单的提醒和事件模型:

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>标签。 查看 http://screencloud.net/img/screenshots/f990e124f356330a10839b00cd92f75a.png

我想要做的是显示Event.name<options>. 那么我在这里错过了什么?

[更新] 更改为form_for& 它生成以下 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>由于某种原因缺少标签。

4

2 回答 2

0

我做错了,这个解决方案不需要 accepts_nested_attributes_for. 删除它并解决问题。

于 2012-10-26T21:20:15.350 回答
0

试试这个:collection_select(:event, :id, Event.all, :id, :name)

于 2012-09-20T02:14:22.603 回答