我無法獲得我的關聯以保存在localhost:3000/controller_name/new
中。我相信這是由於belongs_to
failing validation,但我不知道如何解決它(除了通過requires:false
/optional: true
放棄關聯,其後果我不確定)。我在這個tutorial之後創建了我的關聯,但它是用於以前版本的rails。多態關聯交易在Rails 5中失敗
我有一個多態地址表,可以屬於事件,企業,用戶等我試圖將它添加到事件。
地址遷移 - 你可以看到它引用addressable
:
class CreateAddresses < ActiveRecord::Migration[5.1]
def change
create_table :addresses do |t|
t.string :address
t.decimal :latitude, null: false, precision: 10, scale: 6, index: true
t.decimal :longitude, null: false, precision: 10, scale: 6, index: true
t.references :addressable, polymorphic: true, index: true
end
end
end
地址模式:
class Address < ApplicationRecord
belongs_to :addressable, polymorphic: true
end
事件模型:
class Event < ApplicationRecord
has_one :address, as: :addressable
accepts_nested_attributes_for :address
end
事件控制器:
class EventsController < ApplicationController
#...stuff...
# GET /events/new
def new
@event = Event.new
@event.address = @event.build_address
#@event.address = Address.new(addressable: @event)
#@event.address = @event.create_address
#@event.address = @addressable.User.new
end
#...stuff...
你可以看到我嘗試了多種方法來創建事件的地址,他們主要是創建下面的項目,使用addressable
導致Nil
崩潰。
#<Address id: nil, address: nil, latitude: nil, longitude: nil, addressable_type: "Event", addressable_id: nil>
事件表(使用Simple_form寶石):
<%= simple_form_for @event do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.simple_fields_for :address do |address| %>
<%= render :partial => 'shared/address/form', :locals => {:f => address} %>
<% end %>
<%= f.button :submit %>
<% end %>
地址形式分:
<!-- Google Maps Must be loaded -->
<% content_for :head do %>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCMh8-5D3mJSXspmJrhSTtt0ToGiA-JLBc&libraries=places"></script>
<% end %>
<div id="map"></div>
<%= f.input :address %>
<%= f.input :latitude %>
<%= f.input :longitude %>
形式呈現的罰款。當我試圖保存我得到
Started POST "/events" for 127.0.0.1 at 2017-07-01 16:06:23 -0400
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"R0zlSs3UUNV3x8sQn5ocmE4jP12uOsFza7FezBAuhP4sw2MhF1OhixF8sAfDsLpfMEX7x5rhJ9HZfbKna8ncEA==", "event"=>{"name"=>"asd", "description"
=>"asd", "address_attributes"=>{"address"=>"asd", "latitude"=>"1", "longitude"=>"1"}}, "commit"=>"Create Event"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
而且我保留在new
頁面上。如果我插入一個byebug
爲create
,並打印出@event.errors
它表明:
#<ActiveModel::Errors:0x007fb29c34a9a8 @base=#<Event id: nil, name: "asd", description: "asd", min_users: nil, max_users: nil, start_time: nil, recurring: nil, created_at: nil, upd
ated_at: nil>, @messages={:"address.addressable"=>["must exist"]}, @details={:"address.addressable"=>[{:error=>:blank}]}>
我怎樣才能創建address.addressable?根據一些SO答案的建議,關閉需要驗證的後果是什麼?
我還沒有能夠測試它,但我知道該地址確實有可尋址的類型,我會在裏面。我在原文中包含了使用'@ event.build_address'的輸出。我認爲問題在於這會讓Id空了嗎? –
如果您已經設置了連接,例如has_one或has_many,當您使用@ event.addresses.build(rails提供的方法)時,它會自動處理id,您不必手動插入/鍵入 – widjajayd
我是當我在事件控制器的'new'函數中嘗試'@address = @ event.address.build'時,得到'nil:NilClass'的'undefined method'build'。而'未定義的方法'地址'爲#<事件:0x007f3f288c0820>你的意思是?地址地址='用於'@ event.address.build'。 '@ event.build_address'工作,但傳遞給我的形式'{「id」=> nil,「地址」=>無,「緯度」=>零,「經度」=>零,「addressable_type」= = >「Event」,「addressable_id」=> nil}'。 IDK爲什麼ID不在那裏。 –