Reputation: 33755
When I add something to my cart in my app, I get this error on Heroku:
Started POST "/cart/items" for XX.XXX.XXX.XX at 2013-11-25 17:52:12 +0000
2013-11-25T17:52:12.160587+00:00 app[web.1]:
2013-11-25T17:52:12.160587+00:00 app[web.1]: ActionController::RoutingError (uninitialized constant Cart::CartItemsController):
2013-11-25T17:52:12.160587+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.14/lib/active_support/inflector/methods.rb:230:in `block in constantize'
But when I do the same thing in development, I don't get any issues.
This is my routes.rb
:
namespace :cart do
get '/', to: 'cart#index', as: 'index'
match 'checkout', to: 'cart#checkout', as: 'checkout', via: [:post]
resources :cart_items,
path: :items, as: :items,
only: [:create, :destroy]
end
This is my CartItemsController.rb
:
class CartItemsController < ApplicationController
before_filter :initialize_cart
def create
@cart_item = @cart.add_item(params[:item_id])
redirect_to cart_index_path
end
def destroy
@cart_item = @cart.remove_item(params[:id])
respond_to do |format|
format.html { redirect_to cart_index_path }
format.json { head :no_content }
end
end
end
Thoughts on what could be causing this?
Edit 1:
Add to Cart Form
<%= form_tag(cart_items_path) do %>
<%= hidden_field_tag 'item_id', @item.id %>
<%= submit_tag "Add to Cart (#{number_to_currency(@item.price, precision: 2)})", class: "btn btn-success btn-large" %>
<% end %>
Rake Routes Output
cart_index_path GET /cart(.:format) cart/cart#index
cart_checkout_path POST /cart/checkout(.:format) cart/cart#checkout
cart_items_path POST /cart/items(.:format) cart/cart_items#create
cart_item_path DELETE /cart/items/:id(.:format) cart/cart_items#destroy
Upvotes: 0
Views: 123
Reputation: 76774
Have you tried this:
#app/controllers/cart_items_controller.rb
class Cart::CartItemsController < ApplicationController
#app/controllers/cart_controller.rb
class Cart::CartController < ApplicationController
If you use a namespace, you have to delegate your controllers to the namespace. Don't know why - I learnt this from this tutorial with namespaces
Update
As per the OP's comments, you should also look at these to accompany the above code:
views/cart/cart/index.html.erb
Upvotes: 1
Reputation: 2160
Root should go at the bottom, as it is a catch all.
namespace :cart do
match 'checkout', to: 'cart#checkout', as: 'checkout', via: [:post]
resources :cart_items,
path: :items, as: :items,
only: [:create, :destroy]
get '/', to: 'cart#index', as: 'index'
end
Upvotes: 0