Rails 5: fields_for nested_attributes 不保存(创建控制器失败)
Rails 5: fields_for nested_attributes does not save (fails at create controller)
我正在尝试在我的第一个实现中使用 fields_for 来管理以一种形式部分创建 has_many 关系。此表单部分本身是嵌套资源的一部分
到目前为止,我能够在没有 fields_for 嵌套表单的情况下成功呈现、保存和编辑表单。
当我在 form_for 中包含 fields_for、将参数列入白名单并在 #new 中构建对象时,我在控制台中收到此错误,因为它未能保存并重新呈现#new 视图:
(0.1ms) rollback transaction
如何才能成功保存表单以及 nested_attributes?
routes.rb
....
resources :projects do
resources :step_ones
resources :step_threes
resources :step_twos
resources :step_fours
resources :step_fives
resources :timelines
end
step_four.rb
class StepFour < ApplicationRecord
belongs_to :project
has_many :ios_devices
accepts_nested_attributes_for :ios_devices
end
ios_device.rb
class IosDevice < ApplicationRecord
belongs_to :step_four
end
_form.html.erb
<div>
<%= form_for([@project, @step_four]) do |f| %>
....
<%= f.fields_for :ios_devices do |d| %>
<div class='form-group'>
<%= d.label :full_name, "Name:"%>
<%= d.text_field :full_name %>
<%= d.label :email, "Email:"%>
<%= d.text_field :email %>
<%= d.label :udid, "UDID:"%>
<%= d.text_field :udid %>
<% end %>
<%= hidden_field_tag :project_id, :value => @project.id %>
<div class='row'>
<span class='col-md-6'><%= button_to "Back", project_path(@project), method: :get, class:'btn btn-primary full-wide-button main-btn' %></span>
<span class='col-md-6'><%= f.submit 'Save Data', class: 'btn btn-primary full-wide-button'%></span>
</div>
<% end %>
</div>
step_fours_controller.rb
class StepFoursController < ApplicationController
def new
@project = Project.find(params[:project_id])
@step_four = StepFour.new
3.times { @step_four.ios_devices.build }
end
def create
@step_four = StepFour.new(step_four_params)
@project = Project.find(params[:project_id])
@step_four.ios_devices.each do |d|
puts d.full_name
puts d.email
puts d.udid
end
@step_four.project_id = params[:project_id]
if @step_four.save
flash[:success] = "Step Five Data Saved"
redirect_to @project
else
flash[:danger] = "Data Not Saved. Please Try Again"
render "new"
end
end
def show
@step_four = StepFour.where(project_id: (params[:project_id])).first
end
def update
@step_four = StepFour.where(project_id: (params[:project_id])).first
@project = Project.find(params[:project_id])
if @step_four.update_attributes(step_four_params)
flash[:success] = "Step Four Data Saved"
redirect_to @project
else
flash[:danger] = "Data Not Saved. Please Try Again"
render 'edit'
end
end
def edit
@step_four = StepFour.where(project_id: (params[:project_id])).first
@project = Project.find(params[:project_id])
end
def step_four_params
params.require(:step_four).permit(:iphone_name, :iphone_nickname, :android_name, ios_devices_attributes: [:id, :full_name, :email, :udid])
end
end
在意识到错误出在 save 方法上之后,我试图强制解决这个问题并使用 shebang !
引发异常。我收到了导致我提出这个问题的验证错误:
我需要将 optional: true
添加到 ios_device 模型中的 belongs_to: step_four
,因为我认为该对象尚不存在。
现在一切正常
我正在尝试在我的第一个实现中使用 fields_for 来管理以一种形式部分创建 has_many 关系。此表单部分本身是嵌套资源的一部分
到目前为止,我能够在没有 fields_for 嵌套表单的情况下成功呈现、保存和编辑表单。
当我在 form_for 中包含 fields_for、将参数列入白名单并在 #new 中构建对象时,我在控制台中收到此错误,因为它未能保存并重新呈现#new 视图:
(0.1ms) rollback transaction
如何才能成功保存表单以及 nested_attributes?
routes.rb
....
resources :projects do
resources :step_ones
resources :step_threes
resources :step_twos
resources :step_fours
resources :step_fives
resources :timelines
end
step_four.rb
class StepFour < ApplicationRecord
belongs_to :project
has_many :ios_devices
accepts_nested_attributes_for :ios_devices
end
ios_device.rb
class IosDevice < ApplicationRecord
belongs_to :step_four
end
_form.html.erb
<div>
<%= form_for([@project, @step_four]) do |f| %>
....
<%= f.fields_for :ios_devices do |d| %>
<div class='form-group'>
<%= d.label :full_name, "Name:"%>
<%= d.text_field :full_name %>
<%= d.label :email, "Email:"%>
<%= d.text_field :email %>
<%= d.label :udid, "UDID:"%>
<%= d.text_field :udid %>
<% end %>
<%= hidden_field_tag :project_id, :value => @project.id %>
<div class='row'>
<span class='col-md-6'><%= button_to "Back", project_path(@project), method: :get, class:'btn btn-primary full-wide-button main-btn' %></span>
<span class='col-md-6'><%= f.submit 'Save Data', class: 'btn btn-primary full-wide-button'%></span>
</div>
<% end %>
</div>
step_fours_controller.rb
class StepFoursController < ApplicationController
def new
@project = Project.find(params[:project_id])
@step_four = StepFour.new
3.times { @step_four.ios_devices.build }
end
def create
@step_four = StepFour.new(step_four_params)
@project = Project.find(params[:project_id])
@step_four.ios_devices.each do |d|
puts d.full_name
puts d.email
puts d.udid
end
@step_four.project_id = params[:project_id]
if @step_four.save
flash[:success] = "Step Five Data Saved"
redirect_to @project
else
flash[:danger] = "Data Not Saved. Please Try Again"
render "new"
end
end
def show
@step_four = StepFour.where(project_id: (params[:project_id])).first
end
def update
@step_four = StepFour.where(project_id: (params[:project_id])).first
@project = Project.find(params[:project_id])
if @step_four.update_attributes(step_four_params)
flash[:success] = "Step Four Data Saved"
redirect_to @project
else
flash[:danger] = "Data Not Saved. Please Try Again"
render 'edit'
end
end
def edit
@step_four = StepFour.where(project_id: (params[:project_id])).first
@project = Project.find(params[:project_id])
end
def step_four_params
params.require(:step_four).permit(:iphone_name, :iphone_nickname, :android_name, ios_devices_attributes: [:id, :full_name, :email, :udid])
end
end
在意识到错误出在 save 方法上之后,我试图强制解决这个问题并使用 shebang !
引发异常。我收到了导致我提出这个问题的验证错误:
我需要将 optional: true
添加到 ios_device 模型中的 belongs_to: step_four
,因为我认为该对象尚不存在。
现在一切正常