我在rails表单上遇到问题-它在编辑表单中添加了另一个id参数。 我已经尝试添加带有对象ID的隐藏标签。
服务器日志
Import-CSV
这是编辑表单
app/controllers/products_controller.rb:49:in `update'
Started PATCH "/products/index" for 213.17.150.6 at 2018-06-19 19:21:10 +0000
Cannot render console from 213.17.150.6! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ProductsController#update as JS
Parameters: {"utf8"=>"✓", "/products/18/edit"=>{"id"=>"18", "name"=>"test", "price"=>"3.0", "category_id"=>"55"}, "authenticity_token"=>"DMzH7e8ExK6kvciAWyvAb73Rsj84Py8RIufbP+jxxGmH/n5ITC+XGOqdG2LTw3J7XDTRbOsoAUIXXxLwNkDtKg==", "commit"=>"Edit", "id"=>"index"}
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ? [["id", 0], ["LIMIT", 1]]
Completed 404 Not Found in 3ms (ActiveRecord: 0.3ms)
ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=index):
app/controllers/products_controller.rb:49:in `update'
我的编辑表单在索引页面上(我要从索引页面编辑产品)
答案 0 :(得分:0)
form_for 辅助程序的构建方式如下:form_for(record, options = {}, &block)
您需要先指定记录,然后才能将url作为选项传递。
如果您将路由定义为CRUD,则edit_product_path
是GET请求,因此不应将其用于更新操作。
因此,您可以像这样修改表单:
<% @products.each do |product| %>
<% @categories = Category.all %> #btw, you can define this instance variable inside your controller products#index
<%= form_for product, url: product_path(product), remote: true do |f| %>
#the form
<%= f.submit "Edit", class: "btn btn-light" %>
<% end %>
<% end %>
您不再需要这种隐藏字段。