ruby-on-railshas-manystrong-parametersfields-foraccepts-nested-attributes

Rails 5: fields_for nested_attributes does not save (fails at create controller)


I am trying to work in my first implementation using fields_for to manage creating has_many relationship in one form partial. This form partial itself is part of a nested resource

So far, I am able to render, save and edit the form successfully without the fields_for nested form.

When I include the fields_for in the form_for, white-list the params, and build the objects in #new, I get this error in the console as it failed to save and re renders the #new view:

   (0.1ms)  rollback transaction

What can I do to successfully save the form along with the 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

Solution

  • After realizing that the error was upon the save method, I tried to force the issue and raise an exception with a shebang ! . I received a Validation error that lead me to this question:

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

    I needed to add optional: true to the belongs_to: step_four in the ios_device model since, I believe that the object doesn't exist yet.

    Now everything is working