Search code examples
javascriptruby-on-railsturbolinks

Rails can't delete Javascript issue


I've seen numerous posts about this issue but none of them have helped. As a lot of people, I'm trying to have a delete link to delete heore order_items. When I had Turbolinks working I had no issue with those links, except that because of Turbolinks my pages couldn't load properly. I removed it and now I'm trying to fix my links.

Here's my template: _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 %>

My 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

My application.js:

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

And finally my 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

So far I've tried all of those solutions: - Replace method by data-method - Use <%= javascript_include_tag :application %> into my application.html.erb file - Check if

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

are in my application.js

with the help of those posts for e.g.: I can't delete a post in Rails Can't Edit Or Delete Javascript in Rails

EDIT: when I use this path <td><%= link_to '<i class="fas fa-trash"></i>'.html_safe, order_item_path(@order_item), 'data-method' => :delete %> </td> I have this error : "No route matches {:action=>"update", :controller=>"order_items", :id=>nil}, missing required keys: [:id]"

What am I missing ? It's driving me nuts for days now ...


Solution

  • In your

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

    Your path should look like this order_item_path(@order_item), method: :delete so you point to the destroy action. It also looks like you are using @order_item and it's nil, in your _cart_row.html.haml you have a local variable order_item so you probably should use that.

    Hope it helps