嵌套属性未以rails形式保存

时间:2017-08-01 02:06:21

标签: ruby-on-rails ruby activerecord nested-forms activemodel

我正在尝试在Ruby on Rails中创建一个嵌套表单。表格按预期显示。但是在保存时,不会保存嵌套属性“预订”。

cleaner.rb

class Cleaner < ActiveRecord::Base
  validates_presence_of :first_name
  validates_presence_of :last_name

  validates_presence_of :quality_score
  validates :quality_score, inclusion: 0.0...5.0

  has_many :assignments

  has_many :bookings

  has_many :cities, through: :assignments

  has_many :customers, through: :bookings
end

customer.rb

class Customer < ActiveRecord::Base
  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_uniqueness_of :phone_number
  belongs_to :city
  has_many :bookings
  has_many :cleaners, through: :bookings

  accepts_nested_attributes_for :bookings
end

booking.rb

class Booking < ActiveRecord::Base
  belongs_to :customer
  belongs_to :cleaner

  validates_presence_of :customer
  validates_presence_of :cleaner
  validates_presence_of :date
end

customers_controller.rb

class CustomersController < ApplicationController
  before_action :set_customer, only: %i[show edit update destroy]

  def index
    @customers = Customer.all
  end

  def show; end

  def new
    @customer = Customer.new
  end

  def edit; end

  def create
    @customer = Customer.find_or_initialize_by(phone_number: params[:phone_number])
    @customer.assign_attributes(customer_params)
    respond_to do |format|
      if @customer.save
        format.html { redirect_to @customer, notice: 'Customer was successfully created.' }
        format.json { render :show, status: :created, location: @customer }
      else
        format.html { render :new }
        format.json { render json: @customer.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @customer.update(customer_params)
        format.html { redirect_to @customer, notice: 'Customer was successfully updated.' }
        format.json { render :show, status: :ok, location: @customer }
      else
        format.html { render :edit }
        format.json { render json: @customer.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @customer.destroy
    respond_to do |format|
      format.html { redirect_to customers_url, notice: 'Customer was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

  def set_customer
    @customer = Customer.find(params[:id])
  end

  def customer_params
    params.require(:customer).permit(:first_name, :last_name, :phone_number, :city, :city_id, bookings_attributes: %i[cleaner_id date])
  end
end

HomeController.rb

class HomeController < ApplicationController
  def index
    @customer = Customer.new
    @customer.bookings.build
  end
end

参数

{"utf8"=>"✓", "authenticity_token"=>"c4xo2M4r57+/xBsmcc+7yajpQU13u1kiwmOthx/nP7HiJXJIfS9/OqC0MrWCcaDrSW/xN8UGk2+LVfnUnbTb3A==", "customer"=>{"first_name"=>"adfad", "last_name"=>"fad", "phone_number"=>"9392323", "city_id"=>"1", "bookings_attributes"=>{"0"=>{"cleaner_id"=>"1"}}}, "#<ActionView::Helpers::FormBuilder:0x007f86bec96480>"=>{"date(1i)"=>"2017", "date(2i)"=>"8", "date(3i)"=>"2"}, "commit"=>"Create Customer"}

更新后的表格

<h1>Sign Up Now</h1>

<%= form_for @customer do |f| %>
  <% if @customer.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@customer.errors.count, "error") %> prohibited this customer from being saved:</h2>

      <ul>
      <% @customer.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :first_name %><br>
    <%= f.text_field :first_name %>
  </div>
  <div class="field">
    <%= f.label :last_name %><br>
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :phone_number %><br>
    <%= f.text_field :phone_number %>
  </div>
  <div class="field">
    <%= f.label :city %><br>
    <%= f.select :city_id, options_for_select(City.pluck(:name, :id)) %>
  </div>
  <%= f.fields_for :bookings do |b| %>
    <%= b.date_select :date %>
    <br>
    <%= b.select :cleaner_id, Cleaner.all.pluck(:first_name, :id) %>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

现在失败了2个错误

Bookings customer can't be blank
Bookings date can't be blank

1 个答案:

答案 0 :(得分:1)

使用第一个@customer的迭代,以便rails可以正确读取 我看到了这个例子https://www.sitepoint.com/complex-rails-forms-with-nested-attributes/

 <%= f.fields_for :booking do |ff| %>  
 <%= ff.select :city_id,options_for_select(City.pluck(:name, :id)) %>

我希望它有效交叉手指:)