简体   繁体   中英

How to call a Model Method from a Controller in Rails 3?

In my model I have something like:

class CML < Lead
 def model_method
  #something
 end
end 

and in my Controller:

def recommend
    @var.model_method
    #more stuff
  end

and @var is a CML object, however I keep getting:

undefined method `model_method' for #

Does anyone knows why?

Thanks!

It seems @var is empty at that point

you should use something like (choose one):

@var = Var.find params[:id]
#or
@var = Var.new
#or
@var = Var.create

then you could call your

@var.model_method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM