刚刚在共享服务器上将Rails 2中的应用程序转移到VPS上的Rails 4的最后阶段,但是只停留在一件事上。
我有一个使用Carrierwave上传和显示附加图像的Image模型。 Image模型与其他几个模型具有多态关联。主机和关联模型在组合的多部分表单上一起更新。
该表单非常适合创建和编辑主模型以及创建和附加新图像,但无法编辑图像属性,也没有任何错误可以帮助。
以下是我的图片模型的相关部分:
class Image < ActiveRecord::Base
belongs_to :imagings, polymorphic: true, touch: true
mount_uploader :image, ImageUploader, mount_on: :image_file_name
end
我的评论模型:
class Review < ActiveRecord::Base
has_many :images, as: :imagings, dependent: :destroy
accepts_nested_attributes_for :images, reject_if: lambda { |t| t['image'].nil? }, allow_destroy: true
end
我的评论控制器:
class ReviewsController < ApplicationController
def edit
@review = Review.find(params[:id])
3.times {@review.images.build}
end
def update
@review = Review.find(params[:id])
if @review.update(reviews_params)
redirect_to @review, notice: "Successfully updated review."
else
render :edit
end
end
private
def reviews_params
params.require(:review).permit(:title, :authors, :venue, :startdate, :enddate, :body, :approved, :company, :user_id, { images_attributes: [:title, :credits, :image, :_destroy, :id] })
end
end
我可以使用此设置很好地将图像添加到现有评论或新评论中,但如果我尝试更改现有图像的文本字段,则参数会显示在控制台中,但会被忽略:
Started PATCH "/reviews/492" for 127.0.0.1 at 2014-07-16 11:03:42 +0100
Processing by ReviewsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"HvrsCAXXvO4zIqTBw05qSUwU9I2241DXyilhswEbU9o=", "review"=>{"title"=>"Here's a new review", "authors"=>"", "company"=>"", "venue"=>"", "startdate"=>"2014-07-16", "enddate"=>"2014-07-31", "body"=>"<p>Something</p>", "images_attributes"=>{"0"=>{"title"=>"Doggie", "credits"=>"Icons galore", "_destroy"=>"0", "id"=>"1239"}, "1"=>{"title"=>"", "credits"=>""}, "2"=>{"title"=>"", "credits"=>""}, "3"=>{"title"=>"", "credits"=>""}}, "user_id"=>"4", "approved"=>"1"}, "commit"=>"Update Review", "id"=>"492"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Review Load (0.3ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 LIMIT 1 [["id", 492]]
(12.4ms) BEGIN
Image Load (0.4ms) SELECT "images".* FROM "images" WHERE "images"."imagings_id" = $1 AND "images"."imagings_type" = $2 AND "images"."id" IN (1239) [["imagings_id", 492], ["imagings_type", "Review"]]
(0.2ms) COMMIT
Redirected to http://127.0.0.1:3000/reviews/492
Completed 302 Found in 27ms (ActiveRecord: 13.6ms)
您可以在此处看到credit的images属性如何更改为“Icons galore”,但SQL仅提取链接的图像而不更新它。将此与主模型的以下更新进行对比,主模型执行选择,然后在BEGIN和COMMIT之间进行更新。
Started PATCH "/reviews/492" for 127.0.0.1 at 2014-07-16 11:26:01 +0100
Processing by ReviewsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"HvrsCAXXvO4zIqTBw05qSUwU9I2241DXyilhswEbU9o=", "review"=>{"title"=>"Here's a new review", "authors"=>"", "company"=>"", "venue"=>"", "startdate"=>"2014-07-16", "enddate"=>"2014-07-31", "body"=>"<p>Something else</p>", "images_attributes"=>{"0"=>{"title"=>"Doggie", "credits"=>"Icons", "_destroy"=>"0", "id"=>"1239"}, "1"=>{"title"=>"", "credits"=>""}, "2"=>{"title"=>"", "credits"=>""}, "3"=>{"title"=>"", "credits"=>""}}, "user_id"=>"4", "approved"=>"1"}, "commit"=>"Update Review", "id"=>"492"}
User Load (6.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Review Load (0.3ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 LIMIT 1 [["id", 492]]
(18.1ms) BEGIN
Image Load (0.5ms) SELECT "images".* FROM "images" WHERE "images"."imagings_id" = $1 AND "images"."imagings_type" = $2 AND "images"."id" IN (1239) [["imagings_id", 492], ["imagings_type", "Review"]]
SQL (5.9ms) UPDATE "reviews" SET "body" = $1, "updated_at" = $2 WHERE "reviews"."id" = 492 [["body", "<p>Something else</p>"], ["updated_at", "2014-07-16 10:26:01.455481"]]
(24.8ms) COMMIT
Redirected to http://127.0.0.1:3000/reviews/492
Completed 302 Found in 120ms (ActiveRecord: 56.0ms)
然而,如果我尝试在Rails控制台中进行相同的更新,它有时可以完美地运行:
2.1.1 :062 > review = Review.find(492)
Review Load (0.5ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 LIMIT 1 [["id", 492]]
=> #<Review id: 492, title: "Here's a new review", authors: "", venue: "", startdate: "2014-07-16", enddate: "2014-07-31", body: "<p>Something else</p>", approved: true, created_at: "2014-07-16 09:38:33", updated_at: "2014-07-16 10:26:01", user_id: 4, company: "">
2.1.1 :064 > review.images.first.title = "Doggies galore"
=> "Doggies galore"
2.1.1 :066 > review.save
(0.8ms) BEGIN
SQL (7.5ms) UPDATE "images" SET "title" = $1, "updated_at" = $2 WHERE "images"."id" = 1239 [["title", "Doggies galore"], ["updated_at", "2014-07-16 10:32:39.109784"]]
Review Load (0.3ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 LIMIT 1 [["id", 492]]
SQL (1.7ms) UPDATE "reviews" SET "updated_at" = '2014-07-16 10:32:39.129314' WHERE "reviews"."id" = 492
(1.6ms) COMMIT
=> true
有时不会:
2.1.1 :082 > review = Review.find(492)
Review Load (0.3ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 LIMIT 1 [["id", 492]]
=> #<Review id: 492, title: "Here's a new review", authors: "", venue: "", startdate: "2014-07-16", enddate: "2014-07-31", body: "<p>Something else</p>", approved: true, created_at: "2014-07-16 09:38:33", updated_at: "2014-07-16 10:38:30", user_id: 4, company: "">
2.1.1 :083 > review.images.first.credits = "Icons galore"
Image Load (0.5ms) SELECT "images".* FROM "images" WHERE "images"."imagings_id" = $1 AND "images"."imagings_type" = $2 ORDER BY "images"."id" ASC LIMIT 1 [["imagings_id", 492], ["imagings_type", "Review"]]
=> "Icons galore"
2.1.1 :084 > review.save!
(0.2ms) BEGIN
(0.3ms) COMMIT
=> true
2.1.1 :085 > review.images.first
Image Load (0.8ms) SELECT "images".* FROM "images" WHERE "images"."imagings_id" = $1 AND "images"."imagings_type" = $2 ORDER BY "images"."id" ASC LIMIT 1 [["imagings_id", 492], ["imagings_type", "Review"]]
=> #<Image id: 1239, title: "Doggies", credits: "Icons", created_at: "2014-07-16 09:38:33", updated_at: "2014-07-16 10:38:30", image_file_name: "ddad7b05-3430-49ed-b129-fd484ac793ad.gif", image_content_type: nil, image_file_size: nil, image_updated_at: nil, imagings_id: 492, imagings_type: "Review", original_filename: "Doggie.gif">
2.1.1 :086 >
我知道我错过了一些明显的东西。有人可以帮忙吗?
答案 0 :(得分:0)
我认为你需要在reviews_params
def中做一个小改动,如下所示:
def reviews_params
params.require(:review).permit(:title, :authors, :venue, :startdate, :enddate, :body, :approved, :company, :user_id, images_attributes => [:title, :credits, :image, :_destroy, :id] )
end
这有用吗?
答案 1 :(得分:0)
我认为您不需要花括号。尝试删除它们。通常,嵌套属性以符号形式列出,该符号映射到符号(属性)数组。它们与所讨论的模型的属性仅用逗号分开。这样的事情,
params.require(:review).permit(:title, :authors, :venue, :startdate, :enddate, :body, :approved, :company, :user_id, :images_attributes => [:title, :credits, :image, :_destroy, :id] ) should work.