Topic: Route error

Hello, I'm new with rails ( rails 3.0 ) and i try to realize the depot application from " agile web development with rails ". I've got a problem on the iteration C1, creating a cart ! So I want to create a list product with a button to adding the product on the cart .

I realize my controller store_controller :

class StoreController < ApplicationController
      def index
        @products = Product.a_vendre
      end
      def add_to_cart
        product = Product.find(params[:id])
        @cart = find_cart                   
        @cart.add_product(product)         
      end
 
    private
    def find_cart
            session[:cart] ||= Cart.new
      end
     
end

I realize my view add_to_cart :

<h2>Your Pragmatic Cart</h2>
<ul>
    <% for item in @cart.items %>
        <li><%=h item.title %></li>
    <% end %>
</ul>

And somewhere in the index page i put a button for adding product to cart :

<%= button_to "Add to Cart", :action => 'add_to_cart', :id => product %>

When i push the button, i've a redirection to "http://localhost:3000/store/add_to_cart?id=6", i think it's the good url but i've got the error :

Routing Error

No route matches "/store/add_to_cart"

When i refresh the url, it does what i want, without error...

So, I think the route "store/add_to_cart" is find, but i don't know the error so... probably the button ...

I generate routes with "rake routes " and in routes.rb :

Depot::Application.routes.draw do
  get 'store/index'
  get 'store/add_to_cart'
  resources :products
  resources :store
  end

Please help me, I work with Rails 3.0 ... And sorry if my english is not to perfect, i'm french...


PS : Sorry for the place, if an admin can put it in Controller and view ... !

Last edited by kevin.guilbert62 (2011-08-01 07:27:48)

Re: Route error

I've got the same problem with an other application :

<%= form_for(@news, :url => { :action => "create" , :cat => params[:cat] }) do |f| %>
  <% if @news.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@news.errors.count, "error") %> prohibited this news from being saved:</h2>

      <ul>
      <% @news.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :titre %><br />
    <%= f.text_field :titre %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :image %><br />
    <%= f.text_field :image %>
  </div>
  <div class="actions">
    <%= f.submit "creer"  %>
  </div>

No routes for "news/create", but i've got the route, and the controller, when i refresh it's good ... It's a problem between button and routes, I think !