Def method help

I have problems in getting the value from one def method to another.

For example

For Example:

class UserController < ApplicationController

def dialout(id) # “id” - is taken from database table field
temp = id
end

def speak
How can i get the value of temp here
end

end

I created an object for dialout as shown below from which i get the
value id above.

obj = UserController.new
obj.dialout(user.id)

@temp ?

temp is a variable which stores the value of id

As Raffael suggested, you would need to use an instance variable
(@temp) rather than a local variable. The local variable would only be
available from the context within which it is called, while the
instance variable would be available instance-wide. This sort of
situation is where a basic understanding of Ruby comes in handy.

A question that I won’t go into, but one that you might want to ask
yourself, is whether or not these methods actually belong in your
controller. Typically controller methods are either actions or private
methods used in filters. Your methods might be better put in either
the helper (if they relate to presentation) or the model (if they
relate to business or database logic).

On Aug 28, 5:57 am, Naveen P. [email protected]

I basically integrate Asterisk with RAGI (Ruby Asterisk Gateway
Interface). I use this program inside the handlers directory which is
the controller for RAGI.

The following is my program:

Code : - fold - unfold

  1. #ivr_handler.rb
  2. require ‘rubygems’
  3. require ‘active_record’
  4. require ‘ragi/call_handler’
  5. require ‘ragi/call_connection’
  6. require ‘action_controller’
  7. class IvrHandler < RAGI::CallHandler
  8. def dialout(arg,id)
  9. @params = {}
  10. #place_call(phoneNumber, callerID, urn, hashData, callDate,
    uniqueID, maxRetries, retryTime, waitTime, extraChannelVars)
  11. @params[:id] = id

RAGI::CallInitiate.place_call(arg,‘naveen’,’/ivr/speak’,@params[:id],nil,‘5’,‘0’,‘1’,‘90’,‘1’)
16. @usermaster = Usermaster.find(id)
17. @usermaster.ivr_status = 1
18. @usermaster.save
19. puts 'title from the table usermaster ---------dailout
-------'[email protected]_s
20.
21. end
22.
23. def speak
24. count = 0
25. flag = true
26. msgr = true
27. while (flag == true)
28.
29. if msgr == true
30. puts ‘This is naveen from the asterisk group.’
31. speak_text (“Hi , This is a reminder on your appointment with
Dr. John on wednesday 23rd of August”)
32. play_sound (“beep”)
33. end
34.
35.
36. speak_text (“To confirm your appointment, Press 1. To request
for reschedule, Press 3. To replay the message from begining, Press 9”)
37. keypresses = get_data(“beep”,5000,1)
38. puts (‘ekyeie
----------------------------------’+keypresses.to_s)
39. if (keypresses == “1”)
40. speak_text ‘Your appointment is confirmed. Have a goodday.’
41. break
42. elsif (keypresses == “3”)
43. speak_text ‘Doctors office will get in touch with you
shortly’
44. break
45. end
46. end
47. flag =false
48. end
49.
50. end

I call the method speak from the asterisk pbx configuration file by
giving the url direction.

usermasters is the table i created in the database and generated a
usermaster.rb model file in the models directory. I use another program
in the lib directory to get the values from database. It is shown below:

Code : - fold - unfold

  1. require “rubygems”
  2. require “active_record”
  3. require “…/app/handlers/ivr_handler”
  4. accessing the database.yml file which is located in config

folder
7.
8. db_config = YAML::load(File.open("…/config/database.yml"))
9.
10. # accessing the database configuration in development mode using
database.yml file
11. ActiveRecord::Base.establish_connection(db_config[‘development’])
12.
13. # access the data using model in app/models
14. require “…/app/models/usermaster”
15.
16. # substracting n number of days from current date
17.
18. #date = DateTime.now - 3
19.
20. # finding users created on the above date
21. @users = Usermaster.find(:all,:conditions =>[“ivr_status=?”,“0”])
22.
23. # creating object for ivrhandler
24. ivr_obj = IvrHandler.new
25.
26. # iterating each user in users table
27. @users.each do |user|
28. # passing phone_number to dialout method in ivrhandler class
29.
30. ivr_obj.dialout(user.contact_number.to_s,user.id)
31. end

I hope this explanation is more clear enough to you. Please suggest me
now how will the speak method get the id from usermaster table. Iam
already passing the value of id to dialout method, you can see that in
the ivr_handler.rb file.

Thanks and appreciate your response.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs