I'm new to Twilio and coding in general, and I'm having trouble accessing params from the Gather method. I'm using Sinatra to code this app. Basically, my app is trying to do the following:
User inputs their number and a delay to a form.
My twilio number will call that number after the specified delay.
A voice will ask the user to input a number using the Gather method.
Call will be redirected to a new url where the voice will count from 1 up to the number inputted.
I want a history log displayed below where it shows the phone number and delay from the form, and the number param gathered during the call. I can see the digits inputed in the Twilio log but don't know how to access it. What's the best way to go about displaying this info in my index.erb file?
<% unless #calls.nil? %>
<% #calls[1..10].each do |call| %>
<tr>
<td><%= call.start_time %> </td>
<td><%= call.to_formatted %> </td>
<td><%= delay input %></td>
<td><%= number inputted %></td>
</tr>
<% end %>
<% end %>
Thanks in advance.
Twilio developer evangelist here.
You can only access the input digits from <Gather> within the call, so if you want to read this out later you will need to save the value in your own database and reconcile the data yourself.
Your app would then look a bit like this:
The action that <Gather>s the digits:
post "/calls" do
twiml = Twilio::TwiML::Response.new do |r|
r.Gather action: "/process_gather" do |g|
g.Say "Please enter a digit and I will count up to it."
end
end
twiml.to_xml
end
Then you can receive the Digits, save them alongside the Call SID, and read out the numbers. (I'm imagining a CallRecord model that acts like ActiveRecord here.)
post "/process_gather" do
number = params["Digits"].to_i
call_record = CallRecord.new(call_sid: params["CallSid"], number: number)
call_record.save!
twiml = Twilio::TwiML::Response.new do |r|
twiml.Say "Here are your numbers."
twiml.Say (1..number).to_a.join(', ')
end
twiml.to_xml
end
Then, for your log page, you can retrieve the calls from the API, as I assume you're doing. You'd then want to return the CallRecords from the database using the CallSids from the API and you can zip the two records together and read out the results into your view.
Does that help at all?
Related
I'm a complete novice in Ruby and Nanoc, but a project has been put in my lap. Basically, the code for the page brings back individual URLs for each item linking them to the manual. I'm trying to create a URL that will list all of the manuals in one search. Any help is appreciated.
Here's the code:
<div>
<%
manuals = #items.find_all('/manuals/autos/*')
.select {|item| item[:tag] == 'suv' }
.sort_by {|item| item[:search] }
manuals.each_slice((manuals.size / 4.0).ceil).each do |manuals_column|
%>
<div>
<% manual_column.each do |manual| %>
<div>
<a href="<%= app_url "/SearchManual/\"#{manual[:search]}\"" %>">
<%= manual[:search] %>
</a>
</div>
<% end %>
</div>
<% end %>
</div>
As you didn't specify what items is returning, I did an general example:
require 'uri'
# let suppose that your items query has the follow output
manuals = ["Chevy", "GMC", "BMW"]
# build the url base
url = "www.mycars.com/search/?list_of_cars="
# build the parameter that will be passed by the url
manuals.each do |car|
url += car + ","
end
# remove the last added comma
url.slice!(-1)
your_new_url = URI::encode(url)
# www.mycars.com/?list_of_cars=Chevy,GMC,BMW
# In your controller, you will be able to get the parameter with
# URI::decode(params[:list_of_cars]) and it will be a string:
# "Chevy,GMC,BMW".split(',') method to get each value.
Some considerations:
I don't know if you are gonna use this on view or controller, if will be in view, than wrap the code with the <% %> syntax.
About the URL format, you can find more choices of how to build it in:
Passing array through URLs
When writing question on SO, please, put more work on that. You will help us find a quick answer to your question, and you, for wait less for an answer.
If you need something more specific, just ask and I can see if I can answer.
I need to access all values from Database according to id but i am getting the following output.
Output:
all details are #<PaymentVendor:0x1ee5860>
all details are #<PaymentVendor:0x1f02798>
I am explaining my code below.
#rest_ids=[21,22,23]
#rest_ids.each do |ids|
#pdf_vendor_details = PaymentVendor.where(:id => ids )
puts "all details are #{#pdf_vendor_details}"
end
From the above code i have some array of ids(i.e- #rest_ids).Here my requirement is when the loop will execute as per id the record will fetch and store in the variable #pdf_vendor_details in array.If I wanted to display some value in table then i will be able to do that like below.
table.html.erb:
<table>
<tr>
<td>ID</td>
<td>Receipt No</td>
<td>Amount</td>
</tr>
<% #pdf_vendor_details.each do |details| %>
<tr>
<td><%= details.id %></td>
<td><%= details.Receipt_No %></td>
<td><%= details.Amount %></td>
</tr>
<% end %>
</table>
But doing this way i can not get any value and unable to display data in table.Please help me to access the data from DB which will store in array to display in table.
Try this:
#pdf_vendor_details = PaymentVendor.where(:id => #rest_ids ) #rails 4
#OR
#pdf_vendor_details = PaymentVendor.find_all_by_id(#rest_ids) #rails 3
#pdf_vendor_details.each do |pdf|
puts "all details are: ID => #{pdf.id}, Receipt_No => #{pdf.Receipt_No}, Amount=> #{pdf.Amount}"
end
Your table.html.erb will be not changed
You can try
PaymentVendor.where(id: [21, 22, 23])
which will build a SQL statement like
SELECT * FROM payment_vendors WHERE id IN (21, 22, 23)
I don't see any reason in looping #rest_ids and querying on each iteration
#pdf_vendor_details = PaymentVendor.where(:id => ids )
and in each iteration you are replacing the previous value of #pdf_vendor_details instead of appending to the array.
Instead
#pdf_vendor_details = PaymentVendor.where(id: #rest_ids ) #rails 4
or
#pdf_vendor_details = PaymentVendor.find_all_by_id(#rest_ids) #rails 3
will do. It will send one query to fetch all records of paymentvendor in those ids'.
Update:
If you want to print the contents of the object #pdf_vendor_details in raw form, you could use .inspect or .to_yaml.
For example, puts #pdf_vendor_details.inspect, will print the contents of the object.
Refer this question, to print contents of object.
#rest_ids=[21,22,23]
#pdf_vendor_details = PaymentVendor.where(id: #rest_ids )
puts "All details are, #{#pdf_vendor_details.inspect}"
HELP!!!!
As I am learning rails I am developing this application and I am caught in a few areas.
In my jobs/index.html.erb file All of the jobs are listed for the users regardless if they are admin or employees. The main difference is that if the job is locked, the employee cannot view the details of the job, and if the job is unlocked the details are fully accessible.
My problem is that whenever the image is clicked, the job gets deleted. I am aiming for the image to be clicked ( when logged in as an admin of course) and the lock image changes to the unlocked one and vice versa.
my code in my jobs/index.html.erb file looks like this :
<% if current_user.admin? %>
<td><%= link_to 'Edit', edit_job_path(job) %></td>
<td><%= link_to 'Delete', job, method: :delete, data: {confirm: 'Are you sure?'} %></td>
<td><%= link_to(image_tag(job.locked ? 'unlock.png' : 'lock.png', size: "18x18"), job, method: :delete) %></td>
<% end %>
currently when the image is clocked the job gets deleted. How can I keep the job and simply change the status from locked to unlocked? I believe I do have to create a method in my controller.
#Bala's suggestion seems like it's on the right track to me. Removing method: :delete won't help you change the locked/unlocked status of your jobs, but you'll at least stop deleting your jobs. Check out this page for more info on the architecture of a link_to tag:
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
By including method: :delete in that part of your link_to tag, you're passing it as a part of the (optional) HTML options hash. The link "name" is the locked/unlocked image, your URL will be populated by Rails (you've pointed it to the Job object), and the method your application will exert on the Job object that link is pointing to is delete. If you've left Rails to its typical routing conventions, this means that you're most likely running the JobsController#destroy method.
You'll want to add a route and method to your controller that allows you to toggle the status of your job from locked to unlocked. Something like this:
In your routes.rb file:
match 'jobs/:id/toggle_lock' => 'jobs#toggle_lock', :as => 'toggle'
In your JobsController:
def toggle_lock
#job = Job.find(params[:id])
#job.locked ? #job.locked = false : #job.locked = true
#job.save!
redirect_to jobs_path ## <= or whatever
end
And finally, in your view:
<%= link_to(image_tag(job.locked ? 'unlock.png' : 'lock.png', size: "18x18"), toggle_path(job)) %>
Feel free to adjust the logic however you like!
Rails (version 4) question: I have a table called "sections" and I generate a controller called Sections like this:
rails generate controller Sections index show edit new delete
When I browse to, for example, http://localhost:3000/sections/edit/4 I don't see the prepopulated values (the values from the DB which I wish to edit) in the webform.
I need to add some details. The edit and update methods (found in sections_controller.rb) are created as:
def edit
#sect = Section.find(params[:id])
end
def update
#sect = Section.find(params[:id])
if #sect.update_attributes(sect_params)
redirect_to(:action=>'index')
else
render('edit')
end
end
The update method doesn't require a template file, but edit does. So in the view\sections\edit.html.erb we have:
<%= form_for(:section, :url => {:action => 'update', :id => #section.id}) do |f| %>
<table>
<tr>
<th> Field 1 </th>
<td> <%= f.text_field(:field1) %> </td>
</tr>
<tr>
<th> Field 2 </th>
<td> <%= f.text_field(:field2) %> </td>
</tr>
</table>
<%= submit_tag("Edit section") %>
<% end %>
So the edit mechanism works perfectly, except that I don't see the prepopulated fields in the edit webform (which is inconvenient to say the least, especially in case of a large number of fields).
I already corrected this by renaming the #sect (only from method edit) instance variable to #section. However, I am puzzled and astounded by this solution. Shouldn't I supposedly be allowed to choose any variable name? Is this a Rails bug? Please enlighten me.
(In every other language I was free to choose any variable name as pleased without any repercussions.)
Take a look at the docs for form_for. You aren't giving it the model object it should be creating the form with, you're just basically specifying the object's name with the :section symbol.
Instead, pass the object you want to edit in (form_for(#section...)), that should get you the pre-populated fields you are looking for.
I'm in need of some assistance with Ruby on Rails and Sqlite3 again.
This is what I did four times:
<%= Bullet.select(:content).order('random()').limit(1).collect { |b| b.content } %>
But instead of four different random "bullets" it will do four of the same bullets chosen randomly from the list of bullets I have.
I also have tried select * from bullets order by random() limit 4; in sqlite3 (using the program sqliteman) and have also done select * from bullets order by random() limit 1; four times.
Any idea on how I can do this successfully in Ruby on Rails by accessing the database table and using embedded ruby (.erb)?
This is the answer in case anyone ever looks across this:
Posted by MurifoX (so all credit goes to him/her)
<% random_bullets = Bullet.all %>
<ul>
<% 4.times do %>
<li><%= random_bullets.sample.content %></li>
<% end %>
</ul>