Rails无法删除Javascript问题

时间:2019-03-01 14:42:13

标签: javascript ruby-on-rails turbolinks

我已经看到许多有关此问题的帖子,但没有一个有帮助。 与许多人一样,我正在尝试使用删除链接来删除原有的order_items。 当我使用Turbolinks时,这些链接没有任何问题,除了由于Turbolinks导致我的页面无法正确加载之外。我删除了它,现在我正试图修复我的链接。

这是我的模板: _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 %>

我的路线.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

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

//= 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>时,出现以下错误:“没有路线匹配{:action =>“ update”,:controller =>“ order_items”,:id => nil},必填键:[:id]“

我想念什么?几天来真让我发疯...

2 个答案:

答案 0 :(得分:1)

在您的

{
  "d": {
    "results": [
      {
        "floormap": "[location here]",
        "floormapid": 10,
        "resourcelocation": "[redacted]",
        "office": "[location here]",
        "officeid": 3,
        "office_number": "00-605"
      }
    ]
  }
}

您的路径应类似于<%= link_to "Ajouter au panier", order_item_path(@order_item), class: "btn btn-primary" %> ,因此您指向销毁操作。看起来您正在使用order_item_path(@order_item), method: :delete,而且它是零,在_cart_row.html.haml中,您有一个局部变量@order_item,因此您可能应该使用它。

希望有帮助

答案 1 :(得分:1)

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