《三》Couldn't find Product with ID=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 的 内容:
自己加的路由一定要 加在 map.resource 前面 因为 单资源rest风格路由规则: map.resource
我们定义这样一个规则: map.resource :account
它会帮我们定义怎样的一个路由规则呢? 答案是一个遵循rest风格的路由规则,非常完美。
ruby代码
maps these actions in the accounts controller:
- class accountscontroller < actioncontroller::base
# get new_account_urldef new# return an html form for describing the new accountend# post account_urldef create# create an accountend# get account_urldef show# find and return the accountendget edit_account_url
def edit# return an html form for editing the accountend# put account_urldef update# find and update the accountenddelete account_url
def destroy# delete the accountend- end
所以 原来我 list 功能不能用就是 我自己加的路由 加在 它后面被它先捕获 url先分析了, 我的公则就没有用了, 改成下面的情况就能正常工作了 [list] [ map.connect 'products/list', :controller => 'products', :action => 'list' map.resources :products
] [/list]