簡體   English   中英

在表單中更新子模型時,父模型對象ID為nil

[英]Parent model Object ID nil when updating child model in form

我正在嘗試使用聯接表Council_history保存父模型ID及其子屬性,出於某種原因,當我嘗試使用以下方法執行類似操作時,它似乎沒有將父模型ID保存在聯接表中,而是保存了子屬性ID。多對多關聯一切正常,並且保存了父對象,但我不確定如果我的關聯有誤,是否已經嘗試了多種組合,將不勝感激。

Started PUT "/properties/34/build/council" for 127.0.0.1 at 2013-08-21 00:44:37 +0100
Processing by Properties::BuildController#update as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"+nfsCrRvTUrgYrdHix0v2QGaDJj25j4X1sh8oHVpkgs=", "property"=>    {"council_history_attributes"=>{"property_id"=>"34", "council_id"=>"1"}}, "commit"=>"Create    Council history", "property_id"=>"34", "id"=>"council"}



Started GET "/properties/34/build/confirmed" for 127.0.0.1 at 2013-08-21 00:44:37 +0100
Processing by Properties::BuildController#show as HTML
  Parameters: {"property_id"=>"34", "id"=>"confirmed"}
  Property Load (0.2ms)  SELECT "properties".* FROM "properties" WHERE "properties"."id" = ? LIMIT 1  [["id", "34"]]

  CouncilHistory Load (0.3ms)  SELECT "council_histories".* FROM "council_histories" WHERE "council_histories"."property_id" = 34 LIMIT 1
   (0.1ms)  begin transaction
   **(0.5ms)  UPDATE "council_histories" SET "property_id" = NULL, "updated_at" = '2013-08-20 23:44:37.220619' WHERE "council_histories"."id" = 18**
   (1.5ms)  commit transaction
  Tenant Load (0.3ms)  SELECT "tenants".* FROM "tenants" WHERE "tenants"."property_id" = 34
  Rendered properties/build/confirmed.html.erb within layouts/application (2.1ms)
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
   (0.4ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" =   "users_roles"."role_id" WHERE "users_roles"."user_id" = 3 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))

Property.rb

class Property < ActiveRecord::Base
  attr_accessible  :name, :council_history_attributes, :council,
   :property_id, :council_id, :status, :council_history_id
  belongs_to :user

  has_one :council_history 
  accepts_nested_attributes_for :council_history


  has_one :council, through: :council_history, :foreign_key => :council_id
  accepts_nested_attributes_for :council

理事會

class Council < ActiveRecord::Base
  attr_accessible :CouncilEmail, :name, :CouncilTel, :council_histories_attributes


  has_many   :council_histories


  has_many   :properties, :through => :council_histories, :foreign_key => :property_id
end

Council_history.rb-連接表

class CouncilHistory < ActiveRecord::Base
   attr_accessible :council_id, :property_id, :vacant 

   belongs_to :council

   belongs_to :property
end

嵌套控制器

class Properties::BuildController < ApplicationController
  include Wicked::Wizard

   steps :council, :confirmed 

  def show
    @property = Property.find(params[:property_id])    
    @property.build_council_history do |council_history| 
    @council = council_history.build_council 
    end
    render_wizard
  end

查看表格

<%= simple_form_for @property, url: wizard_path, :method => 'put' do |f|  %>

  <%= f.simple_fields_for :council_history do |builder| %>
  <%= builder.input :property_id, as: :hidden, input_html: { value: @property.id } %> 
      <%= builder.input :council_id, :collection => Council.all  %>

      <%= builder.submit %>
    <% end %>
<% end %>

如果您將來會問其他問題,請確保避免在問題中加入authenticity_token ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM