我目前正在尝试为我的rails应用程序创建一个购物车,一旦填充我的购物车应该保存在会话中。
每次我尝试运行服务器来测试是否工作,似乎我的路由根本不工作…
见下面我的路线。Rb,我的购物车控制器和索引。
任何帮助都将不胜感激。
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"
devise_for :admins
resources :catalogues
devise_for :users
车控制器
def index
sessions[:cart] = {}
render 'cart/index'
end
def success
end
def add
@catalogue = Catalogue.find(params[:id])
cart = sessions[:cart]
cart = {:catalogue => @catalogue.name,category => @catalogue.category, :code => @catalogue.code , :colour=> @catalogue.colour, :description => @catalogue.description, :image => @catalogue.image , :unitprice => += @catalogue.unitprice, :unitquantity => +=1, :unitweight => += @catalogue.unitweight }
sessions[:cart] = cart
render 'cart/add'
end
end
和我的索引页
<h1>Listing catalogues</h1>
<table>
<tr>
<th>Name</th>
<th>Code</th>
<th>Category</th>
<th>Description</th>
<th>Unitprice</th>
<th>Unitquantity</th>
<th>Unitweight</th>
<th>Colour</th>
<th>Image</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<% @catalogues.each do |catalogue| %>
<tr>
<td><%= catalogue.name %></td>
<td><%= catalogue.code %></td>
<td><%= catalogue.category %></td>
<td><%= catalogue.description %></td>
<td><%= catalogue.unitprice %></td>
<td><%= catalogue.unitquantity %></td>
<td><%= catalogue.unitweight %></td>
<td><%= catalogue.colour %></td>
<td><%= image_tag(catalogue.image, :width => 150) if catalogue.image.present?%></td>
<td><%= catalogue.user.email %></td>
<td><%= link_to 'Show', catalogue %></td>
<td><%= link_to 'Edit', edit_catalogue_path(catalogue) %></td>
<td><%= link_to 'Destroy', catalogue, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to "Add to cart", controller: "cart", action: "add", id: @catalogue.id, method: :post %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to "View cart", controller: "cart", action: "index" %>
<%= link_to 'New Catalogue', new_catalogue_path %>
在你的路由中。你已经用大写的PUT和DELETE定义了路由。定义像put和delete这样的小写路由,因为它们是ActionDispatch::Routing::Mapper类的方法,用来为你的应用程序设置路由。因为ruby是区分大小写的,你需要用正确的大小写来调用这个方法。