Tôi có một mô hình gọi là Công thức sử dụng đá quý acts-as-taggable-on để thêm thẻ vào công thức nấu ăn. Các hành vi lạ là khi tôi chỉnh sửa và cập nhật công thức thông qua bộ điều khiển, các thẻ công thức được thêm thay vì cập nhật cho phù hợp với thẻ. Có một báo cáo tương tự Github issue nhưng không có câu trả lời từ bất kỳ ai. Phiên bản đá quý hoạt động giống như taggable của tôi là 3.2.6. Ví dụ: công thức của tôi có ba thẻ: fruit
, fruit
và vegetables
. Thẻ fruit
không liên quan, vì vậy tôi đã xóa hai thẻ fruit
này trong trường nhập f.tag_list
của tôi để chỉ bao gồm vegetables
.Hành vi lạ trên đá quý hoạt động giống như taggable-on của việc không xóa các nhãn hiệu
Processing by Users::RecipesController#update as JS
Parameters: {"utf8"=>"✓", "recipe"=>{"image_ids"=>"46746", "cover_image_id"=>"46746",
"name"=>"Cauliflower", "description"=>"this is the description", "preparation_time_hr"=>"0",
"preparation_time_min"=>"50", "servings"=>"4",
"category_ids"=>"", "cuisine_ids"=>"", "tag_list"=>"vegetables", "ingredients"=>....}
người dùng/recipes_controller.rb
def update
if @recipe.update_attributes(recipe_params)
if params[:publish_recipe] && params[:publish_recipe] == "true"
update_status!(:publish!)
end
redirect_to user_recipes_url
else
respond_to do |format|
format.html{ render "edit" }
format.js { render partial: "shared/flash_message", locals: { flash_type: "error", flash_message: "#{@recipe.errors.full_messages.to_sentence}" } }
end
end
end
Các recipe_params
bao gồm tag_list
như các tham số cho phép.
def recipe_params
params.required(:recipe).permit(:name, :category_ids, :cuisine_ids, :tag_list, :preparation_time_hr,
:cover_image_id, :preparation_time_min, :servings, :description, :image_ids, :commentable,
ingredients_attributes: [:id, :name, :_destroy],
instructions_attributes: [:id, :text, :_destroy, image_attributes: [:id, :picture, :_destroy]]
) rescue {}
end
Nhưng kết thúc các taggings cũ không được gỡ bỏ, và cái mới đã được thêm vào danh sách: fruit
, fruit
, vegetables
, vegetables
.
Một điều kỳ lạ nữa là khi tôi cố gắng xem tag_list của công thức, đó là một mảng trống. Đã thử chỉnh sửa tag_list
trong bảng điều khiển đường ray, nó đã thêm một gắn thẻ khác nhưng khi tôi làm r.reload
thì tag_list
nó vẫn là một mảng trống. Bây giờ mối quan hệ giữa các công thức và các thẻ là như vậy:
r = Recipe.find(20980)
[56] pry(main)> r.tag_list
ActsAsTaggableOn::Tag Load (14.9ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 20980 AND `taggings`.`taggable_type` = 'Recipe' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
=> []
[57] pry(main)> r.tags
=> [#<ActsAsTaggableOn::Tag id: 2, name: "fruit", taggings_count: 1138, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:47:20", updated_at: "2015-07-25 15:47:53">,
#<ActsAsTaggableOn::Tag id: 2, name: "fruit", taggings_count: 1138, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:47:20", updated_at: "2015-07-25 15:47:53">,
#<ActsAsTaggableOn::Tag id: 531, name: "vegetables", taggings_count: 21, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:56:15", updated_at: "2015-07-25 15:56:16">,
#<ActsAsTaggableOn::Tag id: 531, name: "vegetables", taggings_count: 21, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:56:15", updated_at: "2015-07-25 15:56:16">,
#<ActsAsTaggableOn::Tag id: 531, name: "vegetables", taggings_count: 21, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:56:15", updated_at: "2015-07-25 15:56:16">]
[58] pry(main)> r.taggings
ActsAsTaggableOn::Tagging Load (35.8ms) SELECT `taggings`.* FROM `taggings` WHERE `taggings`.`taggable_id` = 20980 AND `taggings`.`taggable_type` = 'Recipe'
=> [#<ActsAsTaggableOn::Tagging id: 20408, tag_id: 2, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 03:56:13">,
#<ActsAsTaggableOn::Tagging id: 20409, tag_id: 2, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 03:56:14">,
#<ActsAsTaggableOn::Tagging id: 20410, tag_id: 531, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 04:01:36">,
#<ActsAsTaggableOn::Tagging id: 20411, tag_id: 531, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 04:47:38">,
#<ActsAsTaggableOn::Tagging id: 20412, tag_id: 531, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 04:53:38">]
Các truy vấn cơ sở dữ liệu tag_list
là lạ quá, tại sao các truy vấn bao gồm taggings.tagger_id IS NULL
này?
Tôi vẫn còn mới đối với đá quý này, bất kỳ ý tưởng nào về cách cập nhật thẻ phù hợp bằng cách sử dụng các phương pháp đá quý? Tôi hy vọng tôi có thể tránh việc cập nhật các thẻ bằng cách sử dụng mã của riêng tôi để tránh các vấn đề khác. Lưu ý rằng mô hình Công thức của tôi được phiên bản bằng đá quý paper_trail, tôi hy vọng nó không liên quan đến vấn đề này.
Bạn có thể gửi 'recipe_params' của bạn? – Pavan
@Pavan đã thêm 'recipe_params' vào câu hỏi, cảm ơn – nayiaw