代码之家  ›  专栏  ›  技术社区  ›  deczibru

Ruby on Rails正在改变ID PARAM的形式

  •  0
  • deczibru  · 技术社区  · 6 年前

    我对Rails表单有问题,它在编辑表单中添加了另一个id参数。 我已经尝试添加对象ID为的隐藏标记。

    断流测井

    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'
    

    这是编辑表格

    <% @categories = Category.all %>
    <%= form_for edit_product_path(product), method: :patch, remote: true do |f| %>
      <%= f.hidden_field :id, value: product.id %>
      <%= f.label :name %>
      <%= f.text_field :name, value: product.name, class: "form-control" %>
    
      <%= f.label :price %>
      <%= f.number_field :price, value: product.price, step: 0.01, class: "form-control" %>
    
      <% if Category.any? %>          
        <%= f.label :category_id %>
        <%= f.select :category_id do %>
          <% if product.category_id == nil %>
            <%= content_tag(:option, "Uncategorized", value: nil, :disabled => true, selected:"selected") %>
          <% end %>
          <% @categories.each do |o| %>
            <%= content_tag(:option, o.name, value: o.id) %>
          <% end %>
        <% end %>
      <% end %>
    
      <%= f.label :image %>
      <%= image_tag product.image.url(:edit) if product.image? %>
      <%= f.file_field :image, accept: 'image/png,image/gif,image/jpg,image/jpeg', class: 'form-control' %>
    
      <%= hidden_field_tag :authenticity_token, form_authenticity_token %>
    
      <%= f.submit "Edit", class: "btn btn-light" %>
    
        <script type="text/javascript">
        $('#product_image').bind('change', function() {
          var size_in_mb = this.files[0].size/1024/1024;
          if (size_in_mb > 5)
          alert('Maximum file size is 5MB. Please choose smaller file')
        })
      </script>
    
    <% end %>
    

    我的编辑表单在索引页上(我想从索引页编辑产品)

    1 回复  |  直到 6 年前
        1
  •  0
  •   Sovalina    6 年前

    这个 form_for helper是这样构建的: 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 %>
    

    你不应该再需要这样的隐藏场了。