I’m building a call tracking app, to learn rails and twilio. Right now, I have a form, that when it’s submitted, records the twilio phone number, alongside other parameters to the database, and than buys the phone number.
I had this functioning a bit back, but I changed something that broke my ability to call the parameters in the create action.
Now, when I run the code, I get the error :
Twilio::REST::RequestError in PhonesController#create
Missing PhoneNumber parameter. Please specify the phone number that you would like to purchase
Here’s the form that I submit with :
<% @numbers.each do |number| %>
<%= form_for(@phone) do |f| %>
<%= f.hidden_field :original_number, :value => params[:original_number] %>
<%= f.hidden_field :name, :value => params[:name] %>
<%= f.hidden_field :twilio_number, :value => number.phone_number %>
<div class="found_list">
<div class="found_phone_number">
<%= f.label :twilio_number, number.friendly_name %>
</div>
<div class="choose_found_number">
<%= f.submit "Choose This Number", :class => "btn btn-large btn-success" %>
</div>
</div>
<hr>
<% end %>
<% end %>
Here’s what the debugger is telling me when I hit submit :
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"/qJ6qATbNH2pZwxDPcKGifktWtAA5WrShbqb/2+lBbE=",
"phone"=>{"original_number"=>"6463978797",
"name"=>"Website",
"twilio_number"=>"+16464900447"},
"commit"=>"Choose This Number"}
And here’s my create action
def create
@user = current_user
@phone = @user.phones.build(params[:phone])
client = Twilio::REST::Client.new(@user.twilio_account_sid, @user.twilio_auth_token)
number = client.account.incoming_phone_numbers.create({:phone_number => params[:twilio_number]})
if @phone.save && number.true
flash[:success] = "Phone Number Created!"
redirect_to user_path
else
render new_phone_path
flash[:error] = "It looks like there were errors with the submission"
end
end
If I edit the action and replace params[:twilio_number], with ‘+16464900447’, than it works.
Thanks for sticking through it this far! Do you have thoughts on how I could properly call the twilio_number parameter in the controller? Or, do you think there’s some other problem?
I think your problem came from the wrong format of your params hash, it actually is
So you can see that
params[:twilio_number]doesn’t exist, what exists isparams[:phone][:twilio_number]But of course your solution works as when you’re doing
you’re creating a phone instance with params[:phone] which as the
:twilio_numberkey