商店中的 UrlGenerationError#index
UrlGenerationError in Stores#index
我 运行 我认为是路由错误。我正在创建一个购物车,但不知何故我缺少所需的密钥 [:product_id]。我相信这可能是初学者的错误,没有在某处放置正确的代码。
没有路由匹配 {:action=>"add", :controller=>"carts", :product_id=>nil} 缺少必需的键:[:product_id ]
app/views/stores/index.html.erb:34:in block in _app_views_stores_index_html_erb__2030225986820803304_70197415655620
app/views/stores/index.html.erb:3:in _app_views_stores_index_html_erb__2030225986820803304_70197415655620
Carts_Controller.rb
class CartsController < ApplicationController
def show
cart_ids = $redis.smembers current_user_cart
@cart_products = Product.find(cart_ids)
end
def add
$redis.sadd current_user_cart, params[:product_id]
render json: current_user.cart_count, status: 200
end
def remove
$redis.srem current_user_cart, params[:product_id]
render json: current_user.cart_count, status: 200
end
private
def current_user_cart
"cart#{current_user.id}"
end
def carts_params
params.require(:cart).permit(:product_id)
end
end
Cart.rb
class Cart < ActiveRecord::Base
belongs_to :user
end
Products_Controller.rb
class ProductsController < ApplicationController
def index
@products = Product.all
@order_item = current_order.order_items.new
end
end
Routes.rb
resource :cart, only: [:show] do
put 'add/:product_id', to: 'carts#add', as: :add_to
put 'remove/:product_id', to: 'carts#remove', as: :remove_from
end
Stores/index.html
<%=link_to "", class: "button", data: {target: @cart_action, addUrl: add_to_cart_path(@product_id), removeUrl: remove_from_cart_path(@product_id)} do%>
<%=@cart_action%>
当您执行 add_to_cart_path(@product_id) 时,它会将参数发送为 :id 并且您希望它为 :product_id.
您可以通过两种方式解决这个问题。
方式#1
在你的button/link_to中改变
add_to_cart_path(@product_id)
至:
add_to_cart_path(product_id: @product_id)
方式#2
您可以更新您的控制器和路由以期望并使用参数 id 而不是 product_id
我 运行 我认为是路由错误。我正在创建一个购物车,但不知何故我缺少所需的密钥 [:product_id]。我相信这可能是初学者的错误,没有在某处放置正确的代码。
没有路由匹配 {:action=>"add", :controller=>"carts", :product_id=>nil} 缺少必需的键:[:product_id ]
app/views/stores/index.html.erb:34:in block in _app_views_stores_index_html_erb__2030225986820803304_70197415655620
app/views/stores/index.html.erb:3:in _app_views_stores_index_html_erb__2030225986820803304_70197415655620
Carts_Controller.rb
class CartsController < ApplicationController
def show
cart_ids = $redis.smembers current_user_cart
@cart_products = Product.find(cart_ids)
end
def add
$redis.sadd current_user_cart, params[:product_id]
render json: current_user.cart_count, status: 200
end
def remove
$redis.srem current_user_cart, params[:product_id]
render json: current_user.cart_count, status: 200
end
private
def current_user_cart
"cart#{current_user.id}"
end
def carts_params
params.require(:cart).permit(:product_id)
end
end
Cart.rb
class Cart < ActiveRecord::Base
belongs_to :user
end
Products_Controller.rb
class ProductsController < ApplicationController
def index
@products = Product.all
@order_item = current_order.order_items.new
end
end
Routes.rb
resource :cart, only: [:show] do
put 'add/:product_id', to: 'carts#add', as: :add_to
put 'remove/:product_id', to: 'carts#remove', as: :remove_from
end
Stores/index.html
<%=link_to "", class: "button", data: {target: @cart_action, addUrl: add_to_cart_path(@product_id), removeUrl: remove_from_cart_path(@product_id)} do%>
<%=@cart_action%>
当您执行 add_to_cart_path(@product_id) 时,它会将参数发送为 :id 并且您希望它为 :product_id.
您可以通过两种方式解决这个问题。
方式#1
在你的button/link_to中改变
add_to_cart_path(@product_id)
至:
add_to_cart_path(product_id: @product_id)
方式#2
您可以更新您的控制器和路由以期望并使用参数 id 而不是 product_id