0

我正在尝试在我的第一个实现中使用 fields_for 来管理以一种部分形式创建 has_many 关系。此表单部分本身是嵌套资源的一部分

到目前为止,我能够在没有 fields_for 嵌套表单的情况下成功渲染、保存和编辑表单。

当我在 form_for 中包含 fields_for、将参数列入白名单并在 #new 中构建对象时,我在控制台中收到此错误,因为它无法保存并重新呈现 #new 视图:

   (0.1ms)  rollback transaction

我该怎么做才能成功保存表单以及nested_attributes?

路线.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
4

1 回答 1

0

在意识到错误出在 save 方法上之后,我尝试强制解决问题并使用 shebang 引发异常!。我收到一个验证错误,导致我提出这个问题:

错误:验证失败:图像必须存在,rails-5.0,回形针5

我需要添加optional: truebelongs_to: step_fourios_device 模型中,因为我相信该对象还不存在。

现在一切正常

于 2017-03-15T16:28:37.943 回答