2
我有問題添加一個嵌套模型到窗體。具體而言,此表單部分中沒有任何內容。如何使用Rails 5中的fields_for渲染此表單?
new.html.erb
<%= render 'form' %>
_form.html.erb
...
<% fields_for :bigip do |f| %>
<%= f.text_field :bgname %>
<%= f.text_field :bguser %>
<%= f.text_field :bgpassword %>
<% end %>
...
下面是基本模型和控制器。
pool.rb
class Pool < ApplicationRecord
has_one :bigip, inverse_of: :pool
accepts_nested_attributes_for :bigip, :allow_destroy => true
end
bigip.rb
class Bigip < ApplicationRecord
belongs_to :pool
end
pools_controller.rb
def new
@pool = Pool.new
@pool.build_bigip
end
'<%= f.fields_for:big_ip do | big_ip_form |%> ...'請注意」=「和」f。「。 –