如何修复我的方法“添加”到购物车以填充我在rails 3.2中的购物车?

时间:2014-03-22 15:43:01

标签: ruby-on-rails session

我正在尝试创建一个简单的购物车,它将保存在会话中,但到目前为止还没有为我工作。 返回错误:没有路线匹配[GET]" / cart / 2"

这是我从购物车控制器中添加的

  def index
    session[:cart] = {}
    render 'cart/index'
  end

def add
    @catalogue = Catalogue.find(params[:id])
    cart = session[:cart]
    cart = {:id => Catalogue.find(params[:id]) , :catalogue => @catalogue.name,category => @catalogue.category, :code => @catalogue.code , :colour=> @catalogue.colour, :description => @catalogue.description, :image  => @catalogue.image , :unitprice => @catalogue.unitprice, :unitquantity => += @catalogue.unitquantity, :unitweight => @catalogue.unitweight }
    session[:cart] = cart
    render 'cart/add'
    end`

非常感谢任何帮助。

这是我的佣金路线...... 耙路线

               cart_index GET    /cart/index(.:format)           cart#index
             cart_success GET    /cart/success(.:format)         cart#success
                          PUT    /cart/:id(.:format)             cart#add
                          DELETE /cart/:id(.:format)             cart#remove
                     cart DELETE /cart(.:format)                 cart#clear
            cart_checkout POST   /cart/checkout(.:format)        cart#checkout
        new_admin_session GET    /admins/sign_in(.:format)       devise/sessions
#new
            admin_session POST   /admins/sign_in(.:format)       devise/sessions
#create
    destroy_admin_session DELETE /admins/sign_out(.:format)      devise/sessions
#destroy
           admin_password POST   /admins/password(.:format)      devise/password
s#create
       new_admin_password GET    /admins/password/new(.:format)  devise/password
s#new
      edit_admin_password GET    /admins/password/edit(.:format) devise/password
s#edit
                          PUT    /admins/password(.:format)      devise/password
s#update
cancel_admin_registration GET    /admins/cancel(.:format)        devise/registra
tions#cancel
       admin_registration POST   /admins(.:format)               devise/registra
tions#create
   new_admin_registration GET    /admins/sign_up(.:format)       devise/registra
tions#new
  edit_admin_registration GET    /admins/edit(.:format)          devise/registra
tions#edit
                          PUT    /admins(.:format)               devise/registra
tions#update
                          DELETE /admins(.:format)               devise/registra
tions#destroy
               catalogues GET    /catalogues(.:format)           catalogues#inde
x
                          POST   /catalogues(.:format)           catalogues#crea
te
            new_catalogue GET    /catalogues/new(.:format)       catalogues#new
           edit_catalogue GET    /catalogues/:id/edit(.:format)  catalogues#edit

                catalogue GET    /catalogues/:id(.:format)       catalogues#show

                          PUT    /catalogues/:id(.:format)       catalogues#upda
te
                          DELETE /catalogues/:id(.:format)       catalogues#dest
roy
         new_user_session GET    /users/sign_in(.:format)        devise/sessions
#new
             user_session POST   /users/sign_in(.:format)        devise/sessions
#create
     destroy_user_session DELETE /users/sign_out(.:format)       devise/sessions
#destroy
            user_password POST   /users/password(.:format)       devise/password
s#create
        new_user_password GET    /users/password/new(.:format)   devise/password
s#new
       edit_user_password GET    /users/password/edit(.:format)  devise/password
s#edit
                          PUT    /users/password(.:format)       devise/password
s#update
 cancel_user_registration GET    /users/cancel(.:format)         devise/registra
tions#cancel
        user_registration POST   /users(.:format)                devise/registra
tions#create
    new_user_registration GET    /users/sign_up(.:format)        devise/registra
tions#new
   edit_user_registration GET    /users/edit(.:format)           devise/registra
tions#edit
                          PUT    /users(.:format)                devise/registra
tions#update
                          DELETE /users(.:format)                devise/registra
tions#destroy

最后我添加到购物车按钮

<%= link_to "Add to cart", controller: "cart", action: "add", id: catalogue.id , method: :post %>

编辑:

我已更改链接,将项目从上面添加到我的购物车

<td><%= link_to "Add to cart", add_cart_path(catalogue.id), method: :get %></td>
or 
<td><%= link_to "Add to cart", add_cart_path(catalogue.id), method: :get %></td>

现在收到以下错误:

undefined method `add_cart_path' for #<#<Class:0x5e4f070>:0x400cd40>

Extracted source (around line #36):

33:     <td><%= link_to 'Show', catalogue %></td>
34:     <td><%= link_to 'Edit', edit_catalogue_path(catalogue) %></td>
35:     <td><%= link_to 'Destroy', catalogue, method: :delete, data: { confirm: 'Are you sure?' } %></td>
36:     <td><%= link_to "Add to cart", add_cart_path(catalogue.id), method: :post %></td>
37:     
38:   </tr>
39: <% end %>

Rails.root: C:/Users/Vincent/Desktop/BSHCE4/sewingsupplies - 22-03
Application Trace | Framework Trace | Full Trace

app/views/catalogues/index.html.erb:36:in `block in _app_views_catalogues_index_html_erb__837547684_33595272'
app/views/catalogues/index.html.erb:21:in `each'
app/views/catalogues/index.html.erb:21:in `_app_views_catalogues_index_html_erb__837547684_33595272'
app/controllers/catalogues_controller.rb:7:in `index'

仍然不适合我。

编辑:

的routes.rb

Sewingsupplies::Application.routes.draw do
  # get "cart/index"

  # get "cart/success"

  # put "/cart/:id" => "cart#add"
  # delete "/cart/:id" => "cart#remove"
  # delete "/cart" => "cart#clear"
  # post "/cart/checkout"

  resources :carts do
  member do
    put 'add'
  end
end

  devise_for :admins

  resources :catalogues


  devise_for :users

耙路线:

    cart_index GET    /cart/index(.:format)           cart#index
             cart_success GET    /cart/success(.:format)         cart#success
                          PUT    /cart/:id(.:format)             cart#add
                          DELETE /cart/:id(.:format)             cart#remove
                     cart DELETE /cart(.:format)                 cart#clear
            cart_checkout POST   /cart/checkout(.:format)        cart#checkout
        new_admin_session GET    /admins/sign_in(.:format)       devise/sessions
#new
            admin_session POST   /admins/sign_in(.:format)       devise/sessions
#create
    destroy_admin_session DELETE /admins/sign_out(.:format)      devise/sessions
#destroy
           admin_password POST   /admins/password(.:format)      devise/password
s#create
       new_admin_password GET    /admins/password/new(.:format)  devise/password
s#new
      edit_admin_password GET    /admins/password/edit(.:format) devise/password
s#edit
                          PUT    /admins/password(.:format)      devise/password
s#update
cancel_admin_registration GET    /admins/cancel(.:format)        devise/registra
tions#cancel
       admin_registration POST   /admins(.:format)               devise/registra
tions#create
   new_admin_registration GET    /admins/sign_up(.:format)       devise/registra
tions#new
  edit_admin_registration GET    /admins/edit(.:format)          devise/registra
tions#edit
                          PUT    /admins(.:format)               devise/registra
tions#update
                          DELETE /admins(.:format)               devise/registra
tions#destroy
               catalogues GET    /catalogues(.:format)           catalogues#inde
x
                          POST   /catalogues(.:format)           catalogues#crea
te
            new_catalogue GET    /catalogues/new(.:format)       catalogues#new
           edit_catalogue GET    /catalogues/:id/edit(.:format)  catalogues#edit

                catalogue GET    /catalogues/:id(.:format)       catalogues#show

                          PUT    /catalogues/:id(.:format)       catalogues#upda
te
                          DELETE /catalogues/:id(.:format)       catalogues#dest
roy
         new_user_session GET    /users/sign_in(.:format)        devise/sessions
#new
             user_session POST   /users/sign_in(.:format)        devise/sessions
#create
     destroy_user_session DELETE /users/sign_out(.:format)       devise/sessions
#destroy
            user_password POST   /users/password(.:format)       devise/password
s#create
        new_user_password GET    /users/password/new(.:format)   devise/password
s#new
       edit_user_password GET    /users/password/edit(.:format)  devise/password
s#edit
                          PUT    /users/password(.:format)       devise/password
s#update
 cancel_user_registration GET    /users/cancel(.:format)         devise/registra
tions#cancel
        user_registration POST   /users(.:format)                devise/registra
tions#create
    new_user_registration GET    /users/sign_up(.:format)        devise/registra
tions#new
   edit_user_registration GET    /users/edit(.:format)           devise/registra
tions#edit
                          PUT    /users(.:format)                devise/registra
tions#update
                          DELETE /users(.:format)                devise/registra

2 个答案:

答案 0 :(得分:0)

相应地调整routes.rb文件

答案 1 :(得分:0)

从rake路由输出看来,你没有为cart资源添加成员方法。这样添加:

resources :cards do
  member do
    put 'add'
  end
end

了解路由here