Rails 5:fields_for nested_attributes不保存(在创建控制器失败)

时间:2017-03-15 15:23:15

标签: ruby-on-rails has-many strong-parameters fields-for accepts-nested-attributes

我正在努力在我的第一个实现中使用fields_for来管理在一个表单partial中创建has_many关系。此表单部分本身是嵌套资源的一部分

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

当我在form_for中包含fields_for,白名单params,并在#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

1 个答案:

答案 0 :(得分:0)

在意识到保存方法出错后,我尝试强制解决问题,并使用shebang .settings/org.eclipse.jdt.core.prefs引发异常。我收到了验证错误,引导我提出这个问题:

Error: Validation failed: Images imageable must exist , rails-5.0 , paperclip-5

我需要将org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=80 添加到ios_device模型中的!,因为我相信该对象尚未存在。

现在一切正常