我正在尝试使用Rails4更新ActiveRecord中的accepts_nested_attributes_for字段,但它没有发生。
我的活动课程,
t.string :name
t.string :slug
t.string :description
t.integer :total_capacity
t.string :url
t.integer :invite_only
t.integer :is_public
t.integer :event_type_id
t.integer :event_ticketing
t.integer :company_id
t.integer :client_id
t.attachment :display_photo
和EventTiming类,
t.date :date
t.time :start_time
t.time :end_time
t.integer :event_id
t.integer :user_id
t.integer :company_id
我已经完成了,
在事件类
has_many :event_timings, :dependent => :destroy
accepts_nested_attributes_for :event_timings, allow_destroy: true
在控制器中,
def event_params
params.require(:event).permit!
end
edit.html.erb
<%= f.fields_for :event_timings do |builder| %>
<%= render :partial => 'events/event_timing_fields',
:locals => { :f => builder } %>
<% end %>
更新的日志信息是,
Started PATCH "/events/test-dance" for 127.0.0.1 at 2014-06-24 16:44:46 +0530
Processing by EventsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/O06IfwhCDecm//oGuWwGXAIRaCjwbl0s/7CZucOySU=", "event"=>{"name"=>"Test Dance", "url"=>"www.mink7.com", "event_type_id"=>"", "total_capacity"=>"100", "event_ticketing"=>"0", "event_timings_attributes"=>{"0"=>{"date(1i)"=>"2014", "date(2i)"=>"12", "date(3i)"=>"24", "start_time(1i)"=>"2000", "start_time(2i)"=>"1", "start_time(3i)"=>"1", "start_time(4i)"=>"15", "start_time(5i)"=>"29", "end_time(1i)"=>"2000", "end_time(2i)"=>"1", "end_time(3i)"=>"1", "end_time(4i)"=>"15", "end_time(5i)"=>"29", "user_id"=>"2", "id"=>"1"}}, "description"=>"Come and enjoy...!!!"}, "commit"=>"Next", "id"=>"test-dance"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
Company Load (0.3ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`user_id` = 2
Event Load (0.3ms) SELECT `events`.* FROM `events` WHERE `events`.`slug` = 'test-dance' ORDER BY `events`.`id` ASC LIMIT 1
(0.2ms) BEGIN
EventTiming Load (0.7ms) SELECT `event_timings`.* FROM `event_timings` WHERE `event_timings`.`event_id` = 1 AND `event_timings`.`id` IN (1)
(0.3ms) COMMIT
Redirected to http://localhost:3000/events/test-dance
Completed 302 Found in 21ms (ActiveRecord: 2.2ms)
我的更新功能是直截了当的
def update
respond_to do |format|
if @event.update(event_params)
format.html { redirect_to @event, notice: 'Event was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
请帮帮我!!!