views:

33

answers:

3

I have a model called Action. It looks like this:

class Action < ActiveRecord::Base
  def register_action(email,type)
    @action = new()
    @action.guid = "123456789"
    @action.email = email 
    @action.action = type 

    action.guid if @action.save 
  end
end

If I try and access this class from my user_controller, I get an error. The code I'm trying to use is :

if (@User.save)
  guid = Action.inspect() 
  guid = Action.register_action(@User.email,"REGISTER")
  MemberMailer.deliver_confirmation(@User,guid)
end

Action.inspect() works fine, so I'm guessing that the Action class can be seen, but the line which calls register_action returns the following error:

NoMethodError in UserController#createnew
undefined method `register_action' for #<Class:0x9463a10>
c:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1994:in `method_missing'
E:/Rails_apps/myapp/app/controllers/user_controller.rb:32:in `createnew'

What am I doing wrong?

I'm new to Rails so apologies for being stupid.

A: 

In your register_action method you need to do @action = Action.new instead of @action = new(). Alternatively, you can construct it like this:

@action = Action.new(:guid => "123456789", :email => email, :action => type)

Also, you have defined register_action as an instance method on your Action class, but you're calling it as a class method by using Action.register_action. Change it to:

def self.register_action(email, type)
  ...
end
John Topley
+2  A: 

Change

def register_action(email,type)

to either

def self.register_action(email,type)

or

def Action.register_action(email,type)
Salil
+3  A: 

Problem is on this line:

guid = Action.register_action(@User.email,"REGISTER")

register_action is an instance method, not a class method, so you call it on an instance of the Action class, not the Action class itself.

If you want to define register_action as a class method, you should do so like this:

def self.register_action(email, type)
  # ... Body ...
end
mipadi