[英]Nested Attributes in Nested Attributes
Devise 3.5.2
和Rails 4.2.3
大家好
我覺得這才是機智!
我正在嘗試在我的設計注冊表單上的嵌套屬性內添加嵌套屬性。
基本上,用戶輸入他們的聯系信息-名稱/電話(這已經像超級按鈕一樣工作了)。 聯系人字段是使用“聯系人”模型中的嵌套屬性構建的。
但是,我也希望他們在“所有權”模型中嵌套的5個“區域”中輸入。 區域模型包含一個“ zip”列。 我正在嘗試獲取5個框,這些框將在Zones表中生成5行。 我無法在表單上輸入任何區域。
所以:
Sign Up!
First Name: <input>
Last Name: <input>
Phone: <input>
Zips: <input> <input> <input> <input> <input>
Email: <input>
Password: <input>
...<etc>...
MEMBER
______|___________________
| | |
CONTACT OWNERSHIP (columns)
| ___|___________________________
(columns) | | | | |
ZONE ZONE ZONE ZONE ZONE
| | | | |
(column)(column)(column)(column)(column)
會員型號:
class Member < ActiveRecord::Base
belongs_to :role
has_one :contact
has_one :ownership
has_many :zones, through: :ownership
accepts_nested_attributes_for :contact, :ownership, :zones
(聯系人模型為空)
所有權模型:
class Ownership < ActiveRecord::Base
has_many :zones
accepts_nested_attributes_for :zones
end
區域模型:
class Zone < ActiveRecord::Base
belongs_to :ownership
end
注冊控制器:
def new
build_resource({contact_attributes: {}, ownership_attributes: {zones_attributes: {}}})
respond_with self.resource
end
注冊#新視圖:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.fields_for :contact do |cf| %>
<div class="field">
<%= cf.label :first_name %><br />
<%= cf.text_field :first_name, autofocus: true %>
</div>
<div class="field">
<%= cf.label :last_name %><br />
<%= cf.text_field :last_name %>
</div>
<div class="field">
<%= cf.label :phone %><br />
<%= cf.text_field :phone %>
</div>
<% end %>
<% if apply_domain? %>
<%= f.label 'Zips' %>
<%= f.fields_for :ownership do |of| %>
<%= of.fields_for :zones do |zf| %>
<div class="field">
<%= zf.text_field :zip %>
</div>
<div class="field">
<%= zf.text_field :zip %>
</div>
<div class="field">
<%= zf.text_field :zip %>
</div>
<div class="field">
<%= zf.text_field :zip %>
</div>
<div class="field">
<%= zf.text_field :zip %>
</div>
<% end %>
<% end %>
<% end %>
...
~~~~~~~~~~~~
數據庫遷移:
class CreateZones < ActiveRecord::Migration
def change
create_table :zones do |t|
t.string :zip
t.references :ownership
t.timestamps null: false
end
end
end
~~~~
class CreateContacts < ActiveRecord::Migration
def change
create_table :contacts do |t|
t.string :first_name
t.string :last_name
t.string :phone
t.date :dob
t.string :gender
t.string :street
t.string :city
t.string :state
t.string :zip
t.string :referrer
t.references :member
t.timestamps null: false
end
end
end
~~~~
class CreateOwnerships < ActiveRecord::Migration
def change
create_table :ownerships do |t|
t.references :member
t.timestamps null: false
end
end
end
~~~~
class AddOwnershipIdToMember < ActiveRecord::Migration
def change
add_reference :members, :ownership, index: true
end
end
~~~~
您無需在視圖中重復:zip文本字段5次。 你只需要寫
<%= f.fields_for :ownership do |of| %>
<%= of.fields_for :zones do |zf| %>
<div class="field">
<%= zf.text_field :zip %>
</div>
<% end %>
<% end %>
並在您的控制器中創建對象的新實例,例如
resourсe.build_ownership
(1..5).each { resourсe.ownership.zones.new }
但是存在一個問題,所有權沒有任何ID,因此區域無法與其關聯。 因此,您需要在create
動作中加以注意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.