【问题标题】:Form With Nested Attributes Not Saving Rails 5具有嵌套属性的表单不保存 Rails 5
【发布时间】:2016-10-27 04:14:54
【问题描述】:

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

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

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" = $1 LIMIT $2  [["subdomain", "shawnwilson"], ["LIMIT", 1]]
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["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 %>

我们将不胜感激!

EDIT #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)

【问题讨论】:

  • 您从@client.errors 得到哪个错误?请在此处添加,因为从您的服务器日志看来,由于回滚,您的对象没有保存在数据库中。
  • @ShefaleeChaudhary 我会尽快抓到的
  • 它根本没有在表单中给我任何错误.. 只是说请查看以下问题:但是没有错误存在?
  • @ShefaleeChaudhary 见上面的 byebug 错误
  • 这里的 account_id 是零。你已经为此应用了验证。可能是你忘记分配 account_id

标签: ruby-on-rails nested-forms nested-attributes ruby-on-rails-5


【解决方案1】:

如果您不想关闭相关模型的验证(在某些情况下并不理想),那么您应该像这样设置 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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 做到了,但它仍然没有关联..我已经解决了这个问题但是我发布了我的答案
    • 它不起作用。它只是跳过关联的模型并且不将数据保存到数据库中。
    【解决方案3】:

    我通过将 update_attributes 添加到客户端控制器的 create 方法来解决此问题。像这样:

      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
    

    我不是最好的解决方案,但它可以让它发挥作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多