javascript - Rails 无法删除 Javascript 问题
问题描述
我看过很多关于这个问题的帖子,但没有一个有帮助。作为很多人,我试图有一个删除链接来删除这里的 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
我的应用程序.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 中
在这些帖子的帮助下,例如:我无法在 Rails 中删除帖子 无法在 Rails 中编辑或删除 Javascript
编辑:当我使用此路径时 <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]"
我错过了什么?这几天让我发疯...
解决方案
在你的
<%= link_to "Ajouter au panier", order_item_path(@order_item), class: "btn btn-primary" %>
您的路径应如下所示order_item_path(@order_item), method: :delete
,因此您指向销毁操作。看起来你正在使用@order_item
它并且它是 nil,在你的 _cart_row.html.haml 你有一个局部变量order_item
,所以你可能应该使用它。
希望能帮助到你
推荐阅读
- java - 仅检索 Wikipedia 中的介绍文本
- c# - c#中的XML标签格式问题
- oracle - ORACLE - 查询以获取非特定日期格式的记录
- ruby-on-rails - 使用 Socks 代理的 Ruby on Rails Postgres 远程数据库连接
- c++ - 'WTS_VIRTUAL_CLASS' 未在 wtsapi32.h 中定义
- docker - 使用 docker 时,Omnisharp 静默崩溃
- amazon-web-services - Scrapy + Splash 返回很多 504 Time Out 错误
- java - ResultSet 会与底层数据库一起更新吗?
- docker - 在另一个容器退出后运行 docker 容器
- python-3.x - Luigi 全局变量