尝试通过link_to调用自定义方法时出现路由错误

时间:2010-11-16 18:01:15

标签: ruby-on-rails ruby-on-rails-3 routing

您好我正试图通过以下link_to erb行调用我的方法offer_bid: -

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

但我收到以下路由错误: -

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

我应该在routes.rb文件中明确定义路由????

当我运行“rake routes”时,我有相应的link_to路由如下: -

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 =&gt;的path_name“offer_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 个答案:

答案 0 :(得分:1)

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

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

我上面使用了get因为我不确定你打算如何做这件事,但是假设它是一个得到的,因为它是通过一个链接来的。

Rails guides中有更多信息。