《三》Couldn't find Product with ID=list

老郁闷了 以前很简单的功能现在不成了
[list]
[Couldn't find Product with ID=list

RAILS_ROOT: /home/xia/worktest/shoplet
Application Trace | Framework Trace | Full Trace

/home/xia/jruby-1.5.0/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1616:in `find_one'
/home/xia/jruby-1.5.0/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1599:in `find_from_ids'
/home/xia/jruby-1.5.0/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:619:in `find'
/home/xia/worktest/shoplet/app/controllers/products_controller.rb:18:in `show'

]
[/list]
后来从网上找到 :
[list]
[jruby -S rake routes
(in /home/xia/worktest/shoplet)
products GET /products(.:format) {:controller=>"products", :action=>"index"}
POST /products(.:format) {:controller=>"products", :action=>"create"}
new_product GET /products/new(.:format) {:controller=>"products", :action=>"new"}
edit_product GET /products/:id/edit(.:format) {:controller=>"products", :action=>"edit"}
product GET /products/:id(.:format) {:controller=>"products", :action=>"show"}
PUT /products/:id(.:format) {:controller=>"products", :action=>"update"}
DELETE /products/:id(.:format) {:controller=>"products", :action=>"destroy"}
/:controller/:action/:id
/:controller/:action/:id(.:format) ]
[/list]

原来它不认 list 那就改成 index 吧, 现在还是不明白为什么不能用 list 了
为什么 rake routes 里没有 这个


说说 edit 功能

<% form_tag url_for({ :id => @product,:action=>"update"}) ,:method => :put do %>

<%= render :partial => 'form' %>
<%= submit_tag "Edit" ,:name => nil %>
<% end %>


这里 :method => :put 很重要,如果不写 你就等错吧 特别奇妙的是
它会吧 :action=>"update" 给吞了

所以 delete 也得改 :method => :delete
否则 你怎么都没有办法从 list 上删除 记录
其实因为它没有找到 规则 不给 :method 默认是 post
它找不到 就 会 去掉 action
结果 url 就变成 localhost:3000/products/:id
还会报错


摘录 挂于 routes 的 内容:
[quote]
自己加的路由一定要 加在 map.resource 前面
因为
单资源rest风格路由规则: map.resource

我们定义这样一个规则: map.resource :account

它会帮我们定义怎样的一个路由规则呢? 答案是一个遵循rest风格的路由规则,非常完美。

ruby代码

1. # maps these actions in the accounts controller:
2. class accountscontroller < actioncontroller::base
3. # get new_account_url
4. def new
5. # return an html form for describing the new account
6. end
7.
8. # post account_url
9. def create
10. # create an account
11. end
12.
13. # get account_url
14. def show
15. # find and return the account
16. end
17.
18. # get edit_account_url
19. def edit
20. # return an html form for editing the account
21. end
22.
23. # put account_url
24. def update
25. # find and update the account
26. end
27.
28. # delete account_url
29. def destroy
30. # delete the account
31. end
32. end


所以 原来我 list 功能不能用就是 我自己加的路由 加在 它后面被它先捕获 url先分析了, 我的公则就没有用了, 改成下面的情况就能正常工作了
[list]
[
map.connect 'products/list', :controller => 'products', :action => 'list'
map.resources :products

]
[/list]
[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值