具有嵌套属性的表单不保存 Rails 5

Form With Nested Attributes Not Saving Rails 5

我有一个客户和办公地址模型,我想在创建客户时创建办公地址,所以为此我决定采用嵌套属性方向。

当我尝试使用办公室地址创建客户端时,我在服务器输出中得到了这个,没有给我太多继续下去的信息,也不确定如何继续。

Started POST "/clients" for 127.0.0.1 at 2016-10-26 21:57:06 -0600
Processing by ClientsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"oC4Bwgw8zQrQCGU6RVGXXVwgWGIbOGmyP9gmJYUbyKXVXzgdeRGrp/wMnsmbF6spSeNxTpcHLJx+ZceBKjHxvQ==", "client"=>{"account_id"=>"", "name"=>"Test Client", "client_type"=>"Corp", "client_ident"=>"1234567890", "office_address_attributes"=>{"client_id"=>"", "unit_number"=>"317", "street_number"=>"1717", "street_name"=>"60 st SE", "city"=>"Clagary", "prov_state"=>"Alberta", "postal_zip"=>"T2A7Y7", "country"=>"CA"}}, "commit"=>"Create Client"}
  Account Load (0.1ms)  SELECT  "public"."accounts".* FROM "public"."accounts" WHERE "public"."accounts"."subdomain" =  LIMIT   [["subdomain", "shawnwilson"], ["LIMIT", 1]]
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" =  ORDER BY "users"."id" ASC LIMIT   [["id", 1], ["LIMIT", 1]]
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
  Rendering clients/new.html.erb within layouts/application
  Rendered clients/_form.html.erb (32.8ms)
  Rendered clients/new.html.erb within layouts/application (34.4ms)
  Rendered shared/_signed_in_nav.html.erb (0.7ms)
Completed 200 OK in 109ms (Views: 102.0ms | ActiveRecord: 1.2ms)

因此,当我创建客户端时,我想将客户端与帐户相关联,并将 OfficeAddress 与客户端相关联。

我的客户模型

class Client < ApplicationRecord
  belongs_to :account, required: true
  has_one :office_address
  validates :office_address, presence: true
  accepts_nested_attributes_for :office_address
end

我的办公地址模型

class OfficeAddress < ApplicationRecord
  belongs_to :client, required: true
end

我的客户端控制器

class ClientsController < ApplicationController
  before_action :set_client, only: [:show, :edit, :update, :destroy]

  # GET /clients
  # GET /clients.json
  def index
    @clients = Client.all
  end

  # GET /clients/1
  # GET /clients/1.json
  def show
  end

  # GET /clients/new
  def new
    @client = Client.new
    @client.build_office_address
  end

  # GET /clients/1/edit
  def edit
  end

  # POST /clients
  # POST /clients.json
  def create
    @client = Client.new(client_params)

    respond_to do |format|
      if @client.save
        format.html { redirect_to @client, notice: 'Client was successfully created.' }
        format.json { render :show, status: :created, location: @client }
      else
        format.html { render :new }
        format.json { render json: @client.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /clients/1
  # PATCH/PUT /clients/1.json
  def update
    respond_to do |format|
      if @client.update(client_params)
        format.html { redirect_to @client, notice: 'Client was successfully updated.' }
        format.json { render :show, status: :ok, location: @client }
      else
        format.html { render :edit }
        format.json { render json: @client.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /clients/1
  # DELETE /clients/1.json
  def destroy
    @client.destroy
    respond_to do |format|
      format.html { redirect_to clients_url, notice: 'Client was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_client
      @client = Client.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def client_params
      params.require(:client).permit(:account_id, :name, :client_type, :client_ident, office_address_attributes: [:unit_number, :street_number, :street_name, :city, :prov_state, :postal_zip, :country, :client_id])
    end
end

我的表格

<%= simple_form_for(@client) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :account_id %>
    <%= f.input :name %>
    <%= f.input :client_type %>
    <%= f.input :client_ident %>
  </div>

  <%= f.fields_for :office_address do |oa| %>
    <%= oa.input :client_id %>
    <%= oa.input :unit_number %>
    <%= oa.input :street_number %>
    <%= oa.input :street_name %>
    <%= oa.input :city %>
    <%= oa.input :prov_state %>
    <%= oa.input :postal_zip %>
    <%= oa.input :country %>
  <% end %>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

如有任何帮助,我们将不胜感激!

编辑 # 1 - 添加 Byebug 错误

(byebug) @client.errors
#<ActiveModel::Errors:0x007fb249813488 @base=#<Client id: nil, account_id: nil, name: "Test Client", client_type: "Corp", client_ident: "1234567890", created_at: nil, updated_at: nil>, @messages={}, @details={}>
(byebug)

请按照以下方式更改您的关联:

class OfficeAddress < ApplicationRecord
  belongs_to :client, optional: true
end
  • Rails 5 个关联属于 filed 验证您的客户 ID,因此您的条目正在回滚。

我通过在客户端控制器的创建方法中添加 update_attributes 解决了这个问题。像这样:

  def create
    @client = Client.new(client_params)

    respond_to do |format|
      if @client.save
        ### This is what I added ###
        @client.update_attributes!(account_id: @current_account.id)
        @client.office_address.update_attributes!(client_id: @client.id)

        format.html { redirect_to @client, notice: 'Client was successfully created.' }
        format.json { render :show, status: :created, location: @client }
      else
        puts @client.errors
        format.html { render :new }
        format.json { render json: @client.errors, status: :unprocessable_entity }
      end
    end
  end

我的解决方案不是最好的,但它可以正常工作。

如果您不想关闭相关模型的验证(这在某些情况下并不理想),那么您应该像这样设置 inverse_of

has_one :office_address, inverse_of: :client

inverse_of真的很值得了解,这个博客解释的很好:

https://www.viget.com/articles/exploring-the-inverse-of-option-on-rails-model-associations