迭代E3 | CartsController找不到动作'destroy'

时间:2014-02-06 11:43:07

标签: ruby-on-rails

Hello All

我正在研究使用rails 4的敏捷Web开发。

这是我清理购物车的方法(迭代E3)。

1.在views / cart / show.html.erb

中添加button_to
<%= button_to 'Empty cart', @cart, method: :delete,
data: { confirm: 'Are you sure?' } %>

2.在controllers / carts_controller.rb中添加方法destroy

def destroy
  @cart.destroy if @cart.id == session[:cart_id]
  session[:cart_id] = nil
  respond_to do |format|
    format.html { redirect_to store_url, notice: 'Your cart is currently empty' }
    format.json { head :no_content }
  end
end

3.当我点击清空购物车按钮时,我收到此错误消息“无法找到CartsController的动作'销毁',我已经检查了很多次这些代码。
我想知道是否还有别的东西。你能告诉我怎么解决这个问题吗?

的routes.rb

Depot::Application.routes.draw do
  resources :line_items
  resources :carts
  get "store/index"
  resources :products
  root 'store#index', as: 'store'
end
来自rake测试的5个错误,我认为它是相关的,但我不知道如何。

1) Error:
CartsControllerTest#test_should_create_cart:
AbstractController::ActionNotFound: The action 'create' could not be found for CartsController
    test/controllers/carts_controller_test.rb:21:in `block (2 levels) in <class:CartsControllerTest>'
    test/controllers/carts_controller_test.rb:20:in `block in <class:CartsControllerTest>'

  2) Error:
CartsControllerTest#test_should_destroy_cart:
AbstractController::ActionNotFound: The action 'destroy' could not be found for CartsController
    test/controllers/carts_controller_test.rb:45:in `block (2 levels) in <class:CartsControllerTest>'
    test/controllers/carts_controller_test.rb:43:in `block in <class:CartsControllerTest>'

  3) Error:
CartsControllerTest#test_should_get_index:
ActionView::Template::Error: undefined method `each' for nil:NilClass
    app/views/carts/index.html.erb:13:in `_app_views_carts_index_html_erb__860143545109165463_70364136402640'
    test/controllers/carts_controller_test.rb:9:in `block in <class:CartsControllerTest>'

  4) Error:
CartsControllerTest#test_should_get_new:
ActionView::Template::Error: First argument in form cannot contain nil or be empty
    app/views/carts/_form.html.erb:1:in `_app_views_carts__form_html_erb___571452078619530506_70364136338580'
    app/views/carts/new.html.erb:3:in `_app_views_carts_new_html_erb__717241531793059964_70364164357880'
    test/controllers/carts_controller_test.rb:15:in `block in <class:CartsControllerTest>'

  5) Error:
CartsControllerTest#test_should_update_cart:
AbstractController::ActionNotFound: The action 'update' could not be found for CartsController
    test/controllers/carts_controller_test.rb:38:in `block in <class:CartsControllerTest>'

3 个答案:

答案 0 :(得分:0)

试试这个:

 <%= button_to "Empty cart", {:controller => "carts",:action =>
'destroy', :id => cart.id }, :method => :delete %>

将视图文件夹名称更改为购物车。

答案 1 :(得分:0)

检查:

  • @cart中的实例CartsController#show的名称。
  • resources :carts or resource :cart,在您的config/routes.rb

答案 2 :(得分:0)

经过一段时间的检查,我刚刚解决了这个问题。

如果您在购物车控制器的开头有此代码:

私有

def invalid_cart

   logger.error "Attempt to access invalid cart #{params[:id]}"

   redirect_to store_url, notice: 'Invalid cart'

  end

只需删除私有字,然后将该方法移至您拥有方法“set_cart”和“cart_params”的地方。

它对我有用。