Hemanth
Hemanth

Reputation: 5145

Routing error while trying to call a custom method via a link_to

Hi I'm trying to call my method offer_bid through the following link_to erb line:-

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

But I'm getting the following routing error:-

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

Should I explicitly define a route in my routes.rb file ????

I have the corresponding link_to's route as the following when i run "rake routes":-

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"}    

Notice the path_name corresponding to action=>"offer_bid is just a blank !!! Why is it blank here???
The method which I'm trying to call is the below:-

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

Any explanations and suggestions to achieve my use-case is really appreciated . Thanks in advance.

I'm using rails version 3.01

Upvotes: 0

Views: 680

Answers (1)

theIV
theIV

Reputation: 25774

You will need to add your custom action as a member to the resource.

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

I used get above because I'm not sure how you plan on doing this, but assume it's a get since it's coming through a link.

There's more information in the Rails guides.

Upvotes: 1

Related Questions