我正在使用窗體中的另一個嵌套窗體,我想更新嵌套的元素,但它不工作...rails 3.2嵌套窗體不會調用更新時,我發送它
ENV:3.2的Rails
_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圖片:
ACTIV ity.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錯誤...
你有什麼想法發生了什麼?我嘗試了很多解決方案,但他們不工作。 謝謝。
編輯:我忘了,但在活動的的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
源「 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
你可以分享問題中'update'行動的代碼。此外,添加表單提交時獲得的完整服務器日誌,包括'params'哈希。 –
我編輯我的第一篇文章,當我更新「選項」所有作品(用於選項更新),與「圖片」什麼都沒有...... – Manawasp
也分享您的控制器在問題'update'行動的代碼。 –