【发布时间】:2014-03-22 15:01:41
【问题描述】:
我目前正在尝试为我的 rails 应用程序创建一个购物车,并且一旦填充我的购物车应该保存在会话中。
每次我尝试运行服务器来测试是否正常工作时,似乎我的路由根本不工作......
请参阅下面我的 routes.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 %>
【问题讨论】:
-
在 routes.rb 中使用小写的 put 和 delete 方法,而不是 PUT 和 DELETE
-
嗯,它成功了...现在可以运行服务器了...我原以为放低或放高并删除不会有什么不同
-
检查我的答案,为什么它应该是小写的
标签: ruby-on-rails ruby-on-rails-3 routes