我正在使用其中包含另一个嵌套表单的表单,我想更新嵌套元素,但它不起作用...
环境:Rails 3.2
_form.html.haml
= simple_form_for [:pim, activity]
...
= f.simple_fields_for :pictures do |pic_form|
%div.media
.pull-left.col-md-2
= image_tag pic_form.object.image.mini.url
%div.media-body.col-md-5
= pic_form.input :alt
= pic_form.input :element_id, as: :hidden
= pic_form.input :element_type, as: :hidden
= pic_form.input :_destroy, as: :hidden
活动有X张照片:
activity.rb
has_many :pictures, :as => :element, :dependent => :destroy
...
accepts_nested_attributes_for :pictures, allow_destroy: true
当我修改嵌套元素的输入时,服务器跟踪是:
Picture Load (0.3ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."element_id" = 13579 AND "pictures"."element_type" = 'Activity' AND "pictures"."id" IN (9839, 9840, 9838)
但0更新......和0错误......
你知道发生了什么吗?我尝试了很多解决方案,但它们不起作用。 感谢。
编辑:我忘记了,但是在Activity的form_for中它编辑了两个嵌套表单,第二个表单效果很好.......
修改
********************************************************************************
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"cbOJlat6uT3vIKrb2xaNXaD1osWTHL3435gALI8NDBU=",
"activity"=>{"id"=>"13579",
"name"=>"dw",
"category_id"=>"27",
"address"=>"",
"description"=>"",
"description_source"=>"",
"details"=>"",
"from_price"=>"46",
"pictures_attributes"=> {"0"=>{"source"=>"sq",
"element_id"=>"13579",
"element_type"=>"Activity",
"_destroy"=>"false",
"id"=>"9840"},
"1"=>{"source"=>"str de base",
"element_id"=>"13579",
"element_type"=>"Activity",
"_destroy"=>"false",
"id"=>"9839"},
"2"=>{"source"=>"",
"element_id"=>"13579",
"element_type"=>"Activity",
"_destroy"=>"false",
"id"=>"9838"}},
"csv_id"=>"italy",
"options_attributes"=> {"0"=>{"name"=>"a",
"description"=>"",
"code_pre"=>"",
"code_type"=>"",
"activity_id"=>"13579",
"_destroy"=>"false",
"id"=>"38"}}
},
"commit"=>"Save",
"action"=>"update",
"controller"=>"pim/activities",
"locale"=>"fr",
"id"=>"13579"}
(print generate by `pr params`)
********************************************************************************
Activity Load (0.3ms) SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT 1 [["id", "13579"]]
(0.2ms) BEGIN
Picture Load (0.3ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."element_id" = 13579 AND "pictures"."element_type" = 'Activity' AND "pictures"."id" IN (9840, 9839, 9838)
Option Load (0.2ms) SELECT "options".* FROM "options" WHERE "options"."activity_id" = 13579 AND "options"."id" IN (38)
(0.3ms) UPDATE "activities" SET "ids" = '"csv_id"=>"italy-5" "updated_at" = '2014-04-28 15:23:16.962313' WHERE "activities"."id" = 13579
(0.1ms) UPDATE "options" SET "ids" = '"code_pre"=>"","code_type"=>""', "updated_at" = '2014-04-28 15:23:16.963988' WHERE "options"."id" = 38
(3.7ms) COMMIT
Redirected to http://localhost:3000/fr/pim/activities/13579/edit
source“sq”这是一个新字符串
"pictures_attributes"=> {"0"=>{"source"=>"sq",
source“str de base”它是bdd中的值
"1"=>{"source"=>"str de base",
activities_controller.rb
def update
pr params, decorator: '*'
@activity = Activity.find(params[:id])
params[:activity].delete(:id)
if @activity.update_attributes(params[:activity])
redirect_to edit_pim_activity_path(@activity), flash: { success: I18n.t("pim.activities.saved") }
else
render :new, error: I18n.t("pim.activities.error")
end
end
picture.rb
class Picture < ActiveRecord::Base
require 'carrierwave/orm/activerecord'
include Rails.application.routes.url_helpers
IMAGE_SIZES = {
mini: [100, 100]
}
mount_uploader :image, PictureUploader
belongs_to :element, :polymorphic => true
belongs_to :point, foreign_key: :element_id, foreign_type: 'Point'
after_save :element_changed
def element_changed
if !self.element_type.nil? && self.element_type_changed?
self.image.recreate_versions!
end
end
def to_jq_read
{
"tall" => image.tall.url,
}
end
def to_jq_upload
{
"id" => id,
"name" => read_attribute(:image),
"size" => image.size,
"url" => image.url,
"tiny" => image.tiny.url,
"small" => image.small.url,
"tall" => image.tall.url,
"delete_url" => admin_picture_path(:id => id),
"delete_type" => "DELETE"
}
end
def featured_big_url
self.image.featured_big.url
end
def featured_small_url
self.image.featured_small.url
end
def self.remove_by_params(pictures)
pictures.each do |pic|
picture = Picture.find(pic.second[:id])
picture.destroy
end
end
def self.destroy_by_ids(ids)
ids.each do |id|
if picture = Picture.find(id)
picture.destroy
end
end
end
end
答案 0 :(得分:0)
您是否允许picture_attributes进行质量分配:
attr_accessible :pictures_attributes
了解更多信息:http://apidock.com/rails/ActiveModel/MassAssignmentSecurity/ClassMethods/attr_accessible