Rails & Twilio: Receiving nil when storing texts received from Twilio
Posted
by
Jon Smooth
on Stack Overflow
See other posts from Stack Overflow
or by Jon Smooth
Published on 2012-06-18T14:14:32Z
Indexed on
2012/06/18
15:16 UTC
Read the original article
Hit count: 269
I have set up the request URL in my Twilio account to have it POST to: myurl.com/receivetext. It appears to be successfully posting because when I check the database using the Heroku console I see the following:
Post id: 5, body: nil, from: nil, created_at: "2012-06-14 17:28:01", updated_at: "2012-06-14 17:28:01"
Why is it receiving nil for the body and from attributes? I can't figure out what I'm doing wrong! The created and updated at are storing successfully but the two attributes that I care about continue to be stored as nil.
Here's the Receive Text controller which is receiving the Post request from Twilio:
class ReceiveTextController < ApplicationController
def index
@post=Post.create!(body: params[:Body], from: params[:From])
end
end
EDIT: When I dump the params I receive the following: "{\"controller\"=>\"receive_text\", \"action\"=>\"index\"}"
I attained this by inserting the following into my ReceiveText controller. @params = Post.create!(body: params.inspect, from: "Dumping Params") and then opening up the Heroku console to find the database entry with from = "Dumping Params".
I simulated a Twilio request with a curl with the following command curl -X POST myurl.com/receivetext route -d 'AccountSid=AC123&From=%2B19252411234'
I checked the production database again and noticed that the curl request did work when obtaining the FROM atribute. It stored the following:
params.inspect returned "{\"AccountSid\"=>\"AC123\", \"From\"=>\"+19252411234\", \"co..."
I received a comment stating: "As long as twilio is hitting the same URL with the same method (GET/POST) it should be filling the params array as well" I have no idea how to make this comment actionable. Any help would be greatly appreciated! I'm very new to rails.
Here's my database migration (I have both attributes set to string. I have tried setting it to text and that didn't work either) :
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :body
t.string :from
t.timestamps
end
end
end
Here is my Post model:
class Post < ActiveRecord::Base
attr_accessible :body, :from
end
Routes (everything appears to be routing just fine) :
MovieApp::Application.routes.draw do
get "receive_text/index"
get "pages/home"
get "send_text/send_text_message"
root to: 'pages#home'
match '/receivetext', to: 'receive_text#index'
match '/pages/home', to: 'pages#home'
match '/sendtext', to: 'send_text#send_text_message'
end
Here's my gemfile (incase it helps)
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'badfruit'
gem 'twilio-ruby'
gem 'logger'
gem 'jquery-rails'
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
© Stack Overflow or respective owner