Ruby on rails Rails应用程序-嵌套表单在存在子记录时停止工作
在我的应用程序中有产品和图像,我通过一个简单的管理面板来管理它们。 如果没有图像,我可以成功地将图像添加到产品中,但当我上传至少一个图像时(我可以通过编辑或初始创建),我无法进一步上传或删除现有图像。我只是不明白为什么最初的创建或更新会起作用,但更进一步的是,如果我的路线或控制器出现错误,它会在第一次添加图片时出现,不是吗 当我试图通过编辑将新图片添加到现有产品中,并使用现有图片(如果没有初始图片,则更新会正确执行)时,这是一个错误: 我的表格:Ruby on rails Rails应用程序-嵌套表单在存在子记录时停止工作,ruby-on-rails,nested-forms,Ruby On Rails,Nested Forms,在我的应用程序中有产品和图像,我通过一个简单的管理面板来管理它们。 如果没有图像,我可以成功地将图像添加到产品中,但当我上传至少一个图像时(我可以通过编辑或初始创建),我无法进一步上传或删除现有图像。我只是不明白为什么最初的创建或更新会起作用,但更进一步的是,如果我的路线或控制器出现错误,它会在第一次添加图片时出现,不是吗 当我试图通过编辑将新图片添加到现有产品中,并使用现有图片(如果没有初始图片,则更新会正确执行)时,这是一个错误: 我的表格: =form_for([:admin, @prod
=form_for([:admin, @product]) do |f|
%p
...a lot of simple fields, that are working correctly
.box
%h3 Options
=f.fields_for :options do |builder|
=render 'option_fields', f: builder
%br
=link_to_add_fields 'Add an option', f, :options
=f.fields_for :image, Image.new, html: { multipart: true, id: "fileupload" } do |op|
= op.label 'Load product pictures'
= op.file_field :image, multiple: true, id: 'fileupload', name: "product[images_attributes][][image]", class: 'form-control'
-if @product.images.any?
.box
%h6 Delete existing pictures
%p
=f.fields_for :images do |builder|
=image_tag builder.object.image, width: 120
=builder.label :_destroy, 'Delete picture'
=builder.check_box :_destroy
%br
%hr.short.alt
=f.submit 'Save', id: 'submit-data', class: 'btn btn-success'
=link_to 'Back', admin_path, class: 'btn btn-info'
这是我的产品管理员控制器
class Admin::ProductsController < AdminController
before_action :find_product, only: [:edit, :destroy, :update]
def index
@products = Product.all
end
def edit
end
def new
@product = Product.new
option = @product.options.build
@product.servizations.build
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to admin_products_path
flash[:success] = "Created"
else
render action: :new
end
end
def update
if @product.update!(product_params)
redirect_to edit_admin_product_path(@product)
flash[:success] = "Updated"
else
render action: :edit
end
end
def destroy
if @product.destroy
redirect_to admin_products_path
flash[:info] = 'Deleted
end
end
private
def product_params
params.require(:product).permit(:title, :description, :advertising_text,
:fancy_quote, :hot, :hotpic, :product_size_ids, :material,
{ volume_ids: [] }, { color_ids: [] }, { addservice_ids: [] }, :category_id, :subcategory_id,
options_attributes: [:size, :weight, :price, :material, :product_id, :id],
images_attributes: [ :image, :product_id, :id ],
servizations_attributes: [:id, :product_id, :addservice_id, :coefficient]
)
end
def find_product
@product = Product.find(params[:id])
end
end
更新。根据请求,我的rake路由:
admin_product_images GET /admin/products/:product_id/images(.:format) admin/images#index
POST /admin/products/:product_id/images(.:format) admin/images#create
new_admin_product_image GET /admin/products/:product_id/images/new(.:format) admin/images#new
edit_admin_product_image GET /admin/products/:product_id/images/:id/edit(.:format) admin/images#edit
admin_product_image GET /admin/products/:product_id/images/:id(.:format) admin/images#show
PATCH /admin/products/:product_id/images/:id(.:format) admin/images#update
PUT /admin/products/:product_id/images/:id(.:format) admin/images#update
DELETE /admin/products/:product_id/images/:id(.:format) admin/images#destroy
admin_products GET /admin/products(.:format) admin/products#index
POST /admin/products(.:format) admin/products#create
new_admin_product GET /admin/products/new(.:format) admin/products#new
edit_admin_product GET /admin/products/:id/edit(.:format) admin/products#edit
admin_product GET /admin/products/:id(.:format) admin/products#show
PATCH /admin/products/:id(.:format) admin/products#update
PUT /admin/products/:id(.:format) admin/products#update
DELETE /admin/products/:id(.:format) admin/products#destroy
我看不出有什么问题。获取的错误表示您正在使用该路径的
POST
。它应该是一个补丁
/放置
。您可以发布编辑视图和新视图吗?恐怕它们都很简单:%h1编辑:=@product.title=render'form'
和新建:%h1 Create new product=render'form'
您使用的rails版本是什么?我可以看看相关的rake路由吗
My rails版本:rails-v rails 4.2.4
rake路由我将作为初始问题的更新发布,它们占用了太多的空间。为什么你有嵌套的图像路由,但也允许从表单中创建它们?我看不出有任何问题。获取的错误表示您正在使用该路径的POST
。它应该是一个补丁
/放置
。您可以发布编辑视图和新视图吗?恐怕它们都很简单:%h1编辑:=@product.title=render'form'
和新建:%h1 Create new product=render'form'
您使用的rails版本是什么?我是否可以查看相关的rake路由
My rails版本:rails-v rails 4.2.4
rake路由我将作为初始问题的更新发布,它们占用了太多的空间。为什么您有嵌套的图像路由,但也允许从表单中创建它们?
class Product < ActiveRecord::Base
validates :title, :description, :advertising_text, presence: true
validates :category_id, :subcategory_id, presence: true
mount_uploader :hotpic, HotpicUploader
belongs_to :category
belongs_to :subcategory
has_many :options
has_many :images, dependent: :destroy
has_many :hot_pics
has_many :line_items
has_many :voluminazations
has_many :volumes, through: :voluminazations
has_many :colorizations
has_many :colors, through: :colorizations
has_many :accompanships
has_many :accompanies, through: :accompanships
has_many :servizations
has_many :addservices, through: :servizations
accepts_nested_attributes_for :options
accepts_nested_attributes_for :images
accepts_nested_attributes_for :hot_pics
accepts_nested_attributes_for :servizations
before_destroy :ensure_not_referenced_by_any_line_item
def initialized_servizations # this is the key method
[].tap do |o|
Addservice.all.each do |addservice|
if c = servizations.find { |c| c.addservice_id == addservice.id }
o << c.tap { |c| c.enable ||= true }
else
o << Servization.new(addservice_id: addservice.id)
end
end
end
end
private
# Ensure there are no line items referencing this product
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
end
resources :products do
resources :servizations
resources :colors
resources :options do
resources :option_pics
end
end
namespace :admin do
get '', to: 'dashboard#index', as: '/'
resources :products do
resources :images
end
resources :categories
resources :subcategories
resources :volumes
resources :members
resources :addservices
resources :settings
resources :deliveries
end
admin_product_images GET /admin/products/:product_id/images(.:format) admin/images#index
POST /admin/products/:product_id/images(.:format) admin/images#create
new_admin_product_image GET /admin/products/:product_id/images/new(.:format) admin/images#new
edit_admin_product_image GET /admin/products/:product_id/images/:id/edit(.:format) admin/images#edit
admin_product_image GET /admin/products/:product_id/images/:id(.:format) admin/images#show
PATCH /admin/products/:product_id/images/:id(.:format) admin/images#update
PUT /admin/products/:product_id/images/:id(.:format) admin/images#update
DELETE /admin/products/:product_id/images/:id(.:format) admin/images#destroy
admin_products GET /admin/products(.:format) admin/products#index
POST /admin/products(.:format) admin/products#create
new_admin_product GET /admin/products/new(.:format) admin/products#new
edit_admin_product GET /admin/products/:id/edit(.:format) admin/products#edit
admin_product GET /admin/products/:id(.:format) admin/products#show
PATCH /admin/products/:id(.:format) admin/products#update
PUT /admin/products/:id(.:format) admin/products#update
DELETE /admin/products/:id(.:format) admin/products#destroy