Rails 无法删除 Javascript 问题

Rails can't delete Javascript issue

我看过很多关于此问题的帖子,但其中 none 对我有所帮助。 和很多人一样,我正在尝试使用 delete link 来删除 heore order_items。 当我使用 Turbolinks 时,我对那些 links 没有任何问题,只是因为 Turbolinks 我的页面无法正确加载。我删除了它,现在我正在尝试修复我的 links.

这是我的模板: _cart_row_html.erb

      <%= simple_form_for order_item, authenticity_token: true, remote: true do |f| %>
    <td><%= product.reference %></td>
    <td><%= product.name %></td>
    <td><%= f.number_field :quantity, value: order_item.quantity.to_i, class: "form-control", min: 1 %></td>
    <td><%= f.button :submit, "Mettre à jour la quantité", class: "btn btn-primary" %></td>
    <td><%= link_to '<i class="fas fa-trash"></i>'.html_safe, 'data-method' => :delete %> </td>
    <td><%= number_to_currency order_item.total_price, locale: :fr %></td>
  <% end %>

我的routes.rb:

  Rails.application.routes.draw do
  root to: "home#index"
  devise_for :users, controllers: { registrations: "registrations",  sessions: "sessions" }
  resource :cart, only: [:show]
  resources :order_items, only: [:create, :update, :destroy]
  resources :categories
  resources :products
  get 'orders/validate_cart', as: 'validate_cart'
  get 'orders/validate_coordinates', as: 'validate_coordinates'
  #get 'order_items/create'
  #get 'order_items/update'
  #get 'order_items/destroy'
  get 'carts/show'
  get '/articles/:category', to: 'articles#index', as: 'list_product_by_category'
  get '/articles/:categories/:id', to: 'articles#show', as: 'product_detail'
  get '/users', to: 'users#index',  as: 'users_list'
  get 'dashboard' => 'dashboard#index'
end

我的application.js:

//= require jquery3
//= require rails-ujs
//= require activestorage
//= require popper
//= require bootstrap-sprockets
//= require akame.bundle

最后是我的 oder_items_controller.rb

class OrderItemsController < ApplicationController
  def create
    @order = current_order
    @order_item = @order.order_items.new(order_item_params)
    @order.save
    session[:order_id] = @order.id
    redirect_to request.referrer
  end

  def update
    @order = current_order
    @order_item = @order.order_items.find(params[:id])
    @order_item.update_attributes(order_item_params)
    @order_items = @order.order_items
    @order.save
    redirect_to request.referrer, notice: "La quantité a bien été mise à jour"
  end

  def destroy
    @order = current_order
    @order_item = @order.order_items.find(params[:id])
    @order_item.destroy
    @order_items = @order.order_items
    if @order_items.length <=0
      reset_session
      redirect_to carts_show_path
    else
      redirect_to carts_show_path, notice: "Le produit a bien été supprimé"
    end
  end

  private

  def order_item_params
    params.require(:order_item).permit(:quantity, :product_id)
  end
end

到目前为止,我已经尝试了所有这些解决方案: - 用数据方法替换方法 - 在我的 application.html.erb 文件中使用 <%= javascript_include_tag :application %> - 检查是否

//= require jquery3
//= require rails-u

在我的 application.js

在这些帖子的帮助下,例如:I can't delete a post in Rails Can't Edit Or Delete Javascript in Rails

编辑:当我使用这条路径时 <td><%= link_to '<i class="fas fa-trash"></i>'.html_safe, order_item_path(@order_item), 'data-method' => :delete %> </td> 我有这个错误:"No route matches {:action=>"update", :controller=>"order_items", :id=>nil}, missing required keys : [:id]"

我错过了什么?这几天让我发疯了......

在你的

<%= link_to "Ajouter au panier", order_item_path(@order_item), class: "btn btn-primary" %>

你的路径应该看起来像这样order_item_path(@order_item), method: :delete所以你指向销毁动作。看起来你正在使用 @order_item 并且它是零,在你的 _cart_row.html.haml 中你有一个局部变量 order_item 所以你可能应该使用它。

希望对您有所帮助

我已将我的代码更改为 <%= link_to '<i class="fas fa-trash"></i>'.html_safe, order_item_path(order_item), 'data-method' => :delete %>,它确实有效,即使我很确定我之前测试过它。无论如何,我很高兴它有效。我赞成你的回答。谢谢!