No routes match to action

Hi guys!

I’m doing a shopping cart with the gem: gem act_as_shopping_cart (https://github.com/crowdint/acts_as_shopping_cart)

This gem do not have a good documentation, butis popular.

in my action add_item I’m getting the error:

No route matches {:action=>“add_item”, :id=>nil, :controller=>“comprar”}

This is my controller to items:

class ComprarController < SuperSiteController
  def index
@v = Video.order('created_at asc')
    @n = News.    all
@cat = Category.  all
end

  def show
@v = Video.find(params[:id])
    @n = News.    all
@cat = Category.    all
@cart = Cart.  new
end

  def add_item
@cart = Cart.    create
@product = Video.find(params[:id])

    @cart.add(@product, @product.week_price)
  end

end

the model to my cart:

class Cart < ActiveRecord::Base
    attr_accessible :id, :owner_id, :owner_type, :quantity,
              :item_id, :item_type, :

    price
acts_as_shopping_cart_using :  video
end

the model to items to sell:

class Video < ActiveRecord::Base
    attr_accessible :active, :desc, :embed, :img1, :img1_uid,
            :img2, :img2_uid, :img3, :img3_uid, :img4,
            :img4_uid, :infos, :month_price, :slug, :title,
            :trailer, :views, :week_price, :year_price, :

    category_id
belongs_to :
  category
acts_as_shopping_cart_item_for :
cart
end

and the code in my view “show”:

<% form_for @cart, :url => {:action => “add_item”, :id => @cart.id} do |f| %>

<%= f.submit ‘Add to Cart’ %>

<% end %>

Someone is seeing something that I’m not?

I following the documentation, but still get this error

Add Items

To add an item to the cart you use the add method. You have to send the object and the price of the object as parameters.

So, if you had a Product class, you would do something like this:


@cart = Cart.create
@product = Product.find(1)
@cart.add(@product, 99.99)

`

`

Thanks!

Have you put appropriate entries in routes.rb?

Colin

My routes is like that:

get “entrar/index”

devise_for :users

devise_scope :user do

get “login”, to: “devise/sessions#new”

get “sair” => “devise/sessions#destroy”

get “registrar” => “devise/registrations#new”

get “cadastrar” => “devise/invitations#new”

get ‘perfil’ => ‘devise/registrations#edit’, :as => ‘edit_user_registration’

end

mount Ckeditor::Engine => ‘/ckeditor’

get “contato/index”

get “carrinho/index”

get “registrar/index”

#root to: “home#index”

match ‘/home’ => ‘home#index’

#resources :paginas

match ‘xstreams/:id’ => ‘paginas#show’

match ‘entrar’ => ‘cadastro#index’

resources :noticias, :comprar, :cadastro,

:meusvideos, :usuarios, :perfil, :cart

scope ‘admin’, :module => “admin” do

resources :destaques, :testimoniais, :news, :pages,

:home_settings, :videos, :categories, :login,

:dashboard

match ‘homeconfig’ => ‘config_homes#new’

root to: ‘dashboard#index’

end

Should I put some extra route for my action “add_item” in my routes?

Yes, otherwise you will get 'no route matches' because there is not one specified. You can run rake routes to all the routes that you have specified.

Colin

Thanks man!

How I could declare the route for this action?

Thank you!

I did:

post “comprar/:id” => “comprar#add_item”

and seens to work! But everything I put inside the <% form_for @cart, :url => {:action => “add_item”, :id => @cart.id} do |f| %>

is not showing in the view. I even look for with element inspector, but the form for is not rendering what is inside.

Thank you!

I did:

post "comprar/:id" => "comprar#add_item"

and seens to work! But everything I put inside the <% form_for @cart, :url => {:action => "add_item", :id => @cart.id} do |f| %>

is not showing in the view. I even look for with element inspector, but the form for is not rendering what is inside.

In Rails 3 or 3.1, <% form_for became <%= form_for (see the = sign?). That's what is causing that issue.

Walter