0

I am following this code https://stackoverflow.com/a/17886089/692622, here is my client.rb model file and client_controller.rb file

# app/models/client.rb
before_create :add_unsubscribe_hash

private

def add_unsubscribe_hash
    self.unsubscribe_hash = SecureRandom.hex
end

# app/controllers/clients_controller.rb
def unsubscribe
    client = Client.find_by_unsubscribe_hash(params[:unsubscribe_hash])
    client.update_attribute(:subscription, false)
end

but when I am trying to add clients through /clients/new (I have all the 7 methods in controller file too), I am getting error

undefined local variable or method `add_unsubscribe_hash'

The error is coming while saving client in create method

respond_to do |format|
  if @client.save

any idea what is wrong since everything looks alright

EDIT - I have added the model code at pastebin http://pastebin.com/jkegLsaE

Community
  • 1
  • 1
iCyborg
  • 4,699
  • 15
  • 53
  • 84

1 Answers1

2

Notice that in line 40 of your Pastebin, you've opened a foreach loop that should terminate on line 42, but doesn't. Instead, the foreach loop encompasses the entire add_unsubscribe_hash function declaration, so it's not callable by the :before_create callback.

Resolve this by concluding the loop within the function it should be closed in (and ensure that you remove the extraneous end tag at the end of the file):

# app/models/contact.rb
def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
        Contact.create! row.to_hash
    end
end
zeantsoi
  • 25,857
  • 7
  • 69
  • 61