我想在控制器的新方法使用accepts_nested_attributes_for
通過新的父對象ID LINK_TO
代碼保存父,以及在同一時間子對象:
def new
@project = Project.new
@project.instances.build
end
和形式如下:
<%= simple_form_for(@project) do |f| %>
<%= f.input :name %>
<%= link_to "Add New Instance", new_project_instance_path(@project), id: "new_link", remote: true %>
<% end %>
此路由條目是:
resources :projects do
resources :instances
end
而且要顯示需要的字段instances/_form.html.erb
:
<%= form.simple_fields_for :instances do |i| %>
<%= i.input :user_id %>
<%= i.input :password %>
<%= i.input :service_url %>
<% end %>
這裏的問題project_id
爲:nil
,它給錯誤:
No route matches {:action=>"new", :controller=>"instances", :project_id=>nil} missing required keys: [:project_id]
我需要以某種方式調用<%= render 'cdd_instances/form', form: f %>
,使場在項目細節下面呈現,我應該如何實現?
「路線」的外觀如何? – rony36
更新爲'routes' –
因爲'@project'爲nil,所以你不能在路由中傳遞nil,你應該使用'f.fields_for:instances_attributes' – Gabbar