从显示页面将商品添加到购物车时没有路由匹配错误 rails
No route matches error when adding item to shopping cart from show page rails
我正在尝试将产品展示页面中的商品添加到购物车。当按下添加到购物车按钮时,会在数据库中创建一个 order_item 并将其发送到购物车。我有从索引页面添加到购物车按钮,但是当我从显示页面尝试它时,我收到以下错误并且没有任何内容添加到购物车:
ActionController::RoutingError (No route matches [POST] "/products/5"):
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:20:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.5) lib/rails/engine.rb:518:in `call'
railties (4.2.5) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'
Product/show 页面:
<div class="col-xs-4">
<%= form_for '/order_item', remote: true do |f| %>
<h4 class="text-right">Unit Price: <span style="color: green"><%= number_to_currency @product.price %></span></h4>
<div class="input-group">
<%= f.number_field :quantity, value: 1, class: "form-control", min: 1 %>
<div class="input-group-btn">
<%= f.hidden_field :product_id, value: @product.id %>
<%= f.submit "Add to Cart", class: "btn btn-primary" %>
</div>
</div>
<% end %>
</div>
order_items/create.js.erb
<% if @order.errors.any? || @order_item.errors.any? %>
alert("not valid.")
<% else %>
$(".cart-text").html("<%= escape_javascript(render 'layouts/cart_text') %>")
<% end %>
layouts/cart_text.html.erb
<%= link_to "#{current_order.order_items.size} Items in Cart (#{number_to_currency current_order.subtotal} )", cart_path, class: "btn btn-link" %>
carts/show.html.erb
<div class="shopping-cart">
<%= render "shopping_cart" %>
</div>
carts/_shopping_cart.html.erb
<% if !@order_item.nil? && @order_item.errors.any? %>
<div class="alert alert-danger">
<ul>
<% @order_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% if @order_items.size == 0 %>
<p class="text-center">
There are no items in your shopping cart. Please <%= link_to "go back", root_path %> and add some items to your cart.
</p>
<% else %>
<% @order_items.each do |order_item| %>
<%= render 'carts/cart_row', product: order_item.product, order_item: order_item, show_total: true %>
<% end %>
<% end %>
carts_controller.rb
class CartsController < ApplicationController
def show
@order_items = current_order.order_items
end
end
routes.rb
resources :products
resource :cart, only: [:show]
resources :order_items, only: [:create, :update, :destroy]
我不确定您提到的主页是什么意思,但出现此错误是因为您发出的请求 (POST /products/5
) 没有定义适当的路由。
Rails 提供的“足智多谋”路由映射到以下 HTTP 动词:
我怀疑当您打算向另一个端点发出请求时,您正在向 POST /products/:id
发出请求。
form_for
应该收到一个 ActiveRecord
对象,而不是像您在 form_for '/order_item'
中所做的那样的字符串。此表单可能正在对相同的 url 执行 POST
,这应该是确实不存在的 products/5
。
尝试在 ProductController#show
方法中初始化一个名为 @order_item
的变量,并将表单声明更改为 form_for @order_item do |form|
如果 current_order
在您看来可用(我认为是),从 layouts/cart_text
,我认为您应该这样做:
<div class="col-xs-4">
<%= form_for current_order, remote: true do |f| %>
<h4 class="text-right">Unit Price: <span style="color: green"><%= number_to_currency @product.price %></span></h4>
<div class="input-group">
<%= f.number_field :quantity, value: 1, class: "form-control", min: 1 %>
<div class="input-group-btn">
<%= f.hidden_field :product_id, value: @product.id %>
<%= f.submit "Add to Cart", class: "btn btn-primary" %>
</div>
</div>
<% end %>
</div>
我正在尝试将产品展示页面中的商品添加到购物车。当按下添加到购物车按钮时,会在数据库中创建一个 order_item 并将其发送到购物车。我有从索引页面添加到购物车按钮,但是当我从显示页面尝试它时,我收到以下错误并且没有任何内容添加到购物车:
ActionController::RoutingError (No route matches [POST] "/products/5"):
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:20:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.5) lib/rails/engine.rb:518:in `call'
railties (4.2.5) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'
Product/show 页面:
<div class="col-xs-4">
<%= form_for '/order_item', remote: true do |f| %>
<h4 class="text-right">Unit Price: <span style="color: green"><%= number_to_currency @product.price %></span></h4>
<div class="input-group">
<%= f.number_field :quantity, value: 1, class: "form-control", min: 1 %>
<div class="input-group-btn">
<%= f.hidden_field :product_id, value: @product.id %>
<%= f.submit "Add to Cart", class: "btn btn-primary" %>
</div>
</div>
<% end %>
</div>
order_items/create.js.erb
<% if @order.errors.any? || @order_item.errors.any? %>
alert("not valid.")
<% else %>
$(".cart-text").html("<%= escape_javascript(render 'layouts/cart_text') %>")
<% end %>
layouts/cart_text.html.erb
<%= link_to "#{current_order.order_items.size} Items in Cart (#{number_to_currency current_order.subtotal} )", cart_path, class: "btn btn-link" %>
carts/show.html.erb
<div class="shopping-cart">
<%= render "shopping_cart" %>
</div>
carts/_shopping_cart.html.erb
<% if !@order_item.nil? && @order_item.errors.any? %>
<div class="alert alert-danger">
<ul>
<% @order_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% if @order_items.size == 0 %>
<p class="text-center">
There are no items in your shopping cart. Please <%= link_to "go back", root_path %> and add some items to your cart.
</p>
<% else %>
<% @order_items.each do |order_item| %>
<%= render 'carts/cart_row', product: order_item.product, order_item: order_item, show_total: true %>
<% end %>
<% end %>
carts_controller.rb
class CartsController < ApplicationController
def show
@order_items = current_order.order_items
end
end
routes.rb
resources :products
resource :cart, only: [:show]
resources :order_items, only: [:create, :update, :destroy]
我不确定您提到的主页是什么意思,但出现此错误是因为您发出的请求 (POST /products/5
) 没有定义适当的路由。
Rails 提供的“足智多谋”路由映射到以下 HTTP 动词:
我怀疑当您打算向另一个端点发出请求时,您正在向 POST /products/:id
发出请求。
form_for
应该收到一个 ActiveRecord
对象,而不是像您在 form_for '/order_item'
中所做的那样的字符串。此表单可能正在对相同的 url 执行 POST
,这应该是确实不存在的 products/5
。
尝试在 ProductController#show
方法中初始化一个名为 @order_item
的变量,并将表单声明更改为 form_for @order_item do |form|
如果 current_order
在您看来可用(我认为是),从 layouts/cart_text
,我认为您应该这样做:
<div class="col-xs-4">
<%= form_for current_order, remote: true do |f| %>
<h4 class="text-right">Unit Price: <span style="color: green"><%= number_to_currency @product.price %></span></h4>
<div class="input-group">
<%= f.number_field :quantity, value: 1, class: "form-control", min: 1 %>
<div class="input-group-btn">
<%= f.hidden_field :product_id, value: @product.id %>
<%= f.submit "Add to Cart", class: "btn btn-primary" %>
</div>
</div>
<% end %>
</div>