代码之家  ›  专栏  ›  技术社区  ›  Hemanth

试图通过链接调用自定义方法时发生路由错误

  •  0
  • Hemanth  · 技术社区  · 14 年前

    嗨,我正试图通过以下链接到erb线路调用我的方法报价:-

    <%= link_to "Offer Bid", {:controller => "bids", :action => "offer_bid"},
          :remote => true %>  
    

    但我得到了以下路由错误:-

    No route matches {:action=>"offer_bid", :controller=>"bids"}.
    

    我应该在routes.rb文件中显式定义路由吗????

    当我运行“rake route s”时,我有如下对应的链接到的路由:-

    rake routes | grep bid  
             post_bids GET    /posts/:post_id/bids(.:format)                  {:controller=>"bids", :action=>"index"}  
             post_bids POST   /posts/:post_id/bids(.:format)              {:controller=>"bids", :action=>"create"}
          new_post_bid GET    /posts/:post_id/bids/new(.:format)          {:controller=>"bids", :action=>"new"}
         edit_post_bid GET    /posts/:post_id/bids/:id/edit(.:format)     {:controller=>"bids", :action=>"edit"}
              post_bid GET    /posts/:post_id/bids/:id(.:format)          {:controller=>"bids", :action=>"show"}
              post_bid PUT    /posts/:post_id/bids/:id(.:format)          {:controller=>"bids", :action=>"update"}
              post_bid DELETE /posts/:post_id/bids/:id(.:format)          {:controller=>"bids", :action=>"destroy"}
                              /bids/:bid_id(.:format)                     {:controller=>"bids", :action=>"offer_bid"}    
    

    注意与action=>“offer\u bid”对应的路径名只是一个空白!!! 为什么这里是空白的???
    我试图调用的方法如下:-

    def offer_bid
       @bid = Bid.find(params[:id])
       @post.bid_winner_id = @bid.user_id
       @post.save
       flash[:notice] = "Task offered to @post.user.email"
    end
    

    为实现我的用例所做的任何解释和建议都是非常感谢的。 提前谢谢。

    我使用的是rails版本3.01

    1 回复  |  直到 14 年前
        1
  •  1
  •   theIV    14 年前

    您需要将自定义操作添加为 member 到资源。

    resources :bids do
      member do
        get 'offer_bid'
      end
    end
    

    我用过 get 以上是因为我不知道你打算怎么做,但假设这是一个通过链接得到的。

    有更多的信息在 Rails guides .