ITeye博客

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

xiayw · 2010-07-17 15:38 · 编程语言 · Markdown

老郁闷了 以前很简单的功能现在不成了

后来从网上找到 :

原来它不认 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代码

  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. # post account_url  
    
  8. def create  
    
  9.   # create an account  
    
  10. end  
    
  11. # get account_url  
    
  12. def show  
    
  13.   # find and return the account  
    
  14. end  
    
  15. get edit_account_url

  16. def edit  
    
  17.   # return an html form for editing the account  
    
  18. end  
    
  19. # put account_url  
    
  20. def update  
    
  21.   # find and update the account  
    
  22. end  
    
  23. delete account_url

  24. def destroy  
    
  25.   # delete the account  
    
  26. end  
    
  27. end

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

] [/list]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。