簡體   English   中英

如何在Rails 3中使用“ accepts_nested_attributes_for”?

[英]How to use “accepts_nested_attributes_for” in rails 3?

我正在關注文檔並使用simple_form,formtastic和nested_form寶石進行折磨,但均未成功。 這個非常簡單的示例對我來說是空的:

resort.rb

class Resort < ActiveRecord::Base
  attr_accessible :address, :description, :latitude, :longitude, :name, :phone, :second_name,
                  :resort_type_id, :infrastructure_attributes

  validates_presence_of :name, :address, :phone, :description

  has_many :infrastructures
  belongs_to :resort_type

  accepts_nested_attributes_for :infrastructures
end

Infrastructure.rb

class Infrastructure < ActiveRecord::Base
  attr_accessible :name, :description, :infrastructure_type_id, :resort_id

  belongs_to :resort
  belongs_to :infrastructure_type
end

表格檢視

= form_for @resort do |f|
  = f.fields_for :infrastructures do |i|
    = i.text_field :name

似乎我錯過了一些顯而易見的事情,但無法弄清楚代碼到底出了什么問題。

如果我可能要問的話,您在控制器操作中是否為基礎架構構建了適當的代碼。 類似於3.times {@resort = @ resort.infrastrucures.build}

據我所知,您的表單需要這樣的東西,才能構建適當的嵌套表單內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM