`
xiayw
  • 浏览: 11027 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论

《三》Couldn't find Product with ID=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'

    ]

后来从网上找到 :

    [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 那就改成 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.    
   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先分析了, 我的公则就没有用了, 改成下面的情况就能正常工作了

    [
    map.connect 'products/list', :controller => 'products', :action => 'list'
      map.resources :products

    ]
 



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics