我從Rails 3升級到4,所以我採用了強大的參數。看起來嵌套屬性沒有被成功傳遞。我已經閱讀了幾個相關的SO問題和博客文章,我仍然難住。強參數和嵌套屬性
一個Event
有很多Occurrences
。當我提交表單以創建新的Event
和一個或多個Occurrences
時,我得到「1個錯誤禁止保存此類:發生的起點不能爲空。」然而,start
是不空白,我確認通過查看張貼的參數:
{"utf8"=>"✓",
"authenticity_token"=>"aPRLtUbjW7EMxO2kWSzCEctHYZgvBvwuk2QUymfiwkM=",
"event"=>{"name"=>"some name",
"photo"=>#<ActionDispatch::Http::UploadedFile:0x007f9e9bc95310 @tempfile=# <File:/var/folders/rz/1p7tmbmj2t5fbfv2wjhvwcsh0000gn/T/RackMultipart20140927-52743-10pcxtg>,
@original_filename="10435871_671176211140_3488560836686101357_n.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"event[photo]\"; filename=\"10435871_671176211140_3488560836686101357_n.jpg\"\r\nContent-Type: image/jpeg\r\n">,
"summary"=>"",
"facebook_event_link"=>"",
"occurrences_attributes"=>{"0"=>{"start"=>"09/30/2014",
"_destroy"=>"0"}},
"special"=>"0",
"prose"=>""},
"commit"=>"Create Event"}
下面是模型和控制器的相關章節。
應用程序/模型/ event.rb:
class Event < ActiveRecord::Base
has_many :occurrences, :dependent => :destroy
accepts_nested_attributes_for :occurrences, allow_destroy: true, reject_if: proc {|o| o[:start].blank? }
應用程序/模型/ occurrence.rb:
class Occurrence < ActiveRecord::Base
belongs_to :event
validates_presence_of :start
應用程序/控制器/ events_controller.rb:
class EventsController < ApplicationController
def new
@event = Event.new
@event.occurrences.build
@event.passes.build
end
def create
@event = Event.create(event_params)
if @event.save
redirect_to @event, notice: 'Event was successfully created.'
else
render :action => "new"
end
end
...
private
def event_params
params.require(:event).permit(
:name, :expiration, :prose, :special, :summary, :photo, :link, :price, :student_price, :registration_switch, :facebook_event_link,
:occurrences_attributes => [:id, :start, :end, :_destroy])
end
end
爲什麼是不是有關正在通過正確傳遞的信息?