I defined one of my custom method in PostsController as follows:-
class PostsController < ApplicationController
...<<other methods truncated from display >>
public
def update_bid_winner (winner_id)
@post.bid_winner_id = winner_id
@post.save
end
end
But when I try to call it from some other controller (BidsController in my case). Where Bid is a nesteded resource of post:-
resources :posts do
resources :bids do
member do
get 'offer_bid'
end
end
end
I tried to call my custom method as follows from the bids controller :-
def offer_bid
@post = Post.find(params[:post_id])
@bid = Bid.find(params[:id])
@post.update_bid_winner(@bid.user_id) <<<<<<<<<< Here goes the call
@post.save
redirect_to post_path(@post)
end
But I get an error saying that undefined method update_bid_winner :-
undefined method `update_bid_winner' for #<Post:0xb68114f4>
Help me out. am I doing anything wrong here? If so , please suggest ways to achieve the same !!
Thanks in Advance.
PostsController and Post are two different classes. Notice how @post is a Post object:
@post = Post.find(params[:post_id])Define the method in
app/models/post.rbinstead ofapp/controllers/posts_controller.rb.