how to connect to Twilio API with ruby - ruby

Sorry this is a very basic question so it should be easy to answer!
Using ruby and sinatra, I am trying to connect, via the api, to get details of my calls. The prescribed way to do this by twilio seems to be:
#client = Twilio::REST::Client.new account_sid, auth_token
# Loop over calls and print out a property for each one
#client.account.calls.list.each do |call|
puts call.sid
puts call.from
puts call.to
which works fine and "puts" the data in the terminal. I want to print the results on an HTML page, so I changed the line
#client.account.calls.list.each do |call|
to
#calls = #client.account.calls.list
and removed the last 3 lines of the code block above, ie. all the "puts"
then, attempting to print on my index page I included the following:
<% #calls.each do |call| %>
<h4 style="color: #ff0000;"><%= params['msg'] %></h4>
<ul>
<li> <%= call.from %> </li>
<li> <%= call.to %> </li>
</ul>
<% end %>
The error message says:
undefined method `each' for nil:NilClass
so I am not connecting to twilio it seems even though the code is almost exactly the same as that above which does connect and produce the required results.
Any ideas? All help gratefully received.

In Sinatra, do not use instance variables to store connection objects and similar stuff. Instead of #call, use the set method that enables a user to set such objects to different variables.
The calls.list method, as per the code, is defined in the Twilio::REST::ListResource module. This returns an array and so, the second part of your code (in the index.erb) is correct.
The problem is, when you start using instance variables for storing the connection object, it gets reset in the route and that's what's happening inside the get do .. end block.
Change the code to:
set :client, Twilio::REST::Client.new(account_sid, account_pass)
# Now, this setting can be accessed by settings.client
get '/calls' do
#calls = settings.client.account.calls
erb :index
end
# index.erb
<% #calls.each do |call| %>
...
<% end %>
This should work.

Related

NoMethodError in Posts#new

I'm doing the Getting started with Rails tutorial and when I run the local server from shell I get this:
`NoMethodError in Posts#new` `/_form.html.erb where line #1 raised:
`undefined method `model_name' for NilClass:Class
That is the extracted source (around line #1):
1: <%= form_for #post do |f| %>
2: <% if #post.errors.any? %>
3: <div id="errorExplanation">
4: <h2><%= pluralize(#post.errors.count, "error") %> prohibited
I just started on Ruby on Rails and I can't figure out what is happening. What am I doing wrong?
The error message, you are seeing means that you have some variable that contains a nil object instead of the actual object you expect.
While the error message doesn't explicitly reference this, it is likely your #post variable is nil.
Why is it nil? That's near impossible to say given the code here. Please post your PostsController#new action as well.
The fix is into posts_controller.rb, add next code
def new
#post = Post.new
end
Good luck
// Make sure to use model declaration inside your method to check the error logs
class PostsController < ApplicationController
def new
#post = Post.new
end

String will work but not symbols in Rails

To learn Rails, I am writing a simple guestbook app that does not use a database.
Anyway, this is what my view looks like:
views/guest_book_pages/home.html.erb
<h1>Guest Book</h1>
<%= #userinput %>
<%= form_for(:guestbook) do |f| %>
<%= f.label :input %>
<%= f.text_field :input %>
<%= f.submit "Sign" %>
<% end %>
And the controller looks like this:
controllers/guest_book_pages_controller.rb
class StaticPagesController < ApplicationController
def home
#userinput = params[:guestbook]["input"]
end
end
Whenever I change the "input" to a symbol :input, the application breaks and gives me a warning that says: undefined method `[]' for nil:NilClass
What is the reason for this? Why can't I use a symbol?
update: Now it won't even work with the string. What is going on?
update#2: It works with both symbols and string. The only problem is that it will not load the first time. If I can get the page to load, then either will work. How can I get the page to load?
Action use to be handle something, and render view.
when you inter home, the home action has be called, and no param posted now.
for your code, home action should just be empty, it just to render the home_page.
your handle code should move to some action like sign_in, whitch handle the form post and you can get the params.
The first time you load the page the params var is not set. It is only when you submit your form back that there are params
Try
#userinput = params[:guestbook]["input"] || ''
which will initialize the #userinput to an empty string if the params is not found
edit:
This will check if the params has the key guestbook first, then will either set the instance var userinput to an empty string or the value of [guestbook][input] if it exsists.
If all else fails, the instance var is initialized to an empty string to prevent an error in your view.
if params.has_key?(:guestbook)
#userinput = params[:guestbook]["input"] || ''
else
#userinput = ''
end

DataMapper fetching last record using .last throws no method error if using .any? in view

Apologies for the long wined title, in my app I am trying to retrive e the last record from the database that matches the params I have passed to it.
#goals = Weight.last(:is_goal=>true,:user_id=>#user.id)
In my views I want to run a conditional that checks if there are any present and if it has it will display a div.
<% if #goals.any? %>
<% #goals.each do |goal| %>
<p><%= goal.amount %></p>
<% end %>
<% end %>
But for some reason this throws a no method error NoMethodError at /
undefined method 'any?'. If I change the .last to .all it works
#goals = Weight.all(:is_goal=>true,:user_id=>#user.id)
Is there any reason behind this or have I found a bug?
Well, .last method returns an object, .all method returns an array of objects.
And .any? is an array method. You can't call .any? on an object, it will tell you that there is no method unless you have created one.

How to correct in the Sinatra show block

Sorry, I will not use the specific expression in English.
index.erb
<h1>Hello World.</h1>
<ul>
<li>item1</li>
<li>item2</li>
</ul>
<% capture_content :key do %>
I'm Here.
<% end %>
helpers
def capture_content(key, &block)
#content_hash = {}
#content_hash[key] = block.call # this block contains erb all
end
I just want capture_content in content
I hope expression is correct T_T
If you are looking to write yourself the Sinatra equivalent of the Reails content_for helper then you don't need to.
Because there is an extension called Sinatra::ContentFor which is part of the Sinatra::Contrib project which does what you want.
From the documentation:
Sinatra::ContentFor is a set of helpers that allows you to capture
blocks inside views to be rendered later during the request. The most
common use is to populate different parts of your layout from your
view.

How do I perform inline calculations on two variables within an .erb file?

I have the following .erb view in a Sinatra app:
<% sessions.each do |session| %>
<%= session.balance_beginning %>
<%= session.balance_ending %>
<% end %>
It works as expected, displaying the beginning and ending balances recorded for each session. I would like to calculate the net balances from within the .erb file, but I can't figure out how to do it. I have tried variations of this:
<% sessions.each do |session| %>
<%= session.balance_ending - session.balance_beginning %>
<% end %>
That doesn't work. I receive the following error in Sinatra:
undefined method `-' for nil:NilClass
How do I do what I'm trying to do?
Right #Zabba, in this case I think you would add a method to your Session model so you could call session.net_balance.
Then in your balance_ending and balance_beginning methods you would want to handle nil, either raise an error or return zero if that is valid.

Resources