【发布时间】:2021-08-12 06:01:46
【问题描述】:
我正在使用 Rails 构建一个小型市场应用程序,目前正忙于尝试让我的产品编辑和创建页面工作。我添加了图片上传功能,并使用简单的表单来添加产品的详细信息,但每当我尝试继续编辑时,都会收到以下错误:
The action 'update' could not be found for ProductsController
同时,如果我尝试创建新产品,我会收到不同的错误:
{"seller":["must exist"]}
请看我下面的代码:
-products_controller.rb
def create
@product = Product.create
@product_id = @product.id
if @product.save
render :show, status: :created
else
render json: @product.errors, status: :unprocessable_entity
end
end
end
# PATCH/PUT /products/1 or /products/1.json
def update
@product = Product.update (product_params)
if @product.save
render products:id, status: :created
else
render json: @product.errors, status: :unprocessable_entity
end
end
def product_params
params.require(:product).permit(:title, :description, :price, :buyer_id, :seller_id, :category, :image_url)
end
- edit.html.erb
<%= simple_form_for edit_product_path, url: {action: "update"} do |f| %>
<h1 class="heading">Edit Product</h1>
<%= render 'form', product: @product %>
<% end %>
-form.html.erb
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :price %>
<%= f.input :category, collection: ["footwear", "accessories", "menswear", "womenswear"] %>
<div class="form-group">
<% if product.picture.attached? %>
<%= image_tag product.picture, style: "width: 200px; display: block" %>
<% end %>
<%= f.file_field :picture %>
</div>
</div>
非常感谢我能得到的任何帮助。
【问题讨论】:
标签: ruby-on-rails ruby rails-activestorage simple-form-for