Right now I’m trying to build an app which has the ability to search for phone numbers, buy them, and record the transaction to a database.
The way I thought to do it was to have two controllers– (1) the find_numbers controller takes paramaters and searches for numbers. And (2) the phones controller buys the chosen number and saves the parameters to the database.
Here’s a diagram of what I’m trying to do.
http://www.stepanp.com/search-and-buy.jpg
Though I can search for the numbers, and buy the numbers, I can’t seem to save it to the database.
It redirects me and tells me that I did not fill in the proper fields.
This makes me think that the parameters in find_numbers form aren’t being accepted as the parameters for the create action of the phones controller
here’s find_numbers/show
<%= @numbers.each do |number| %>
<%= form_tag(:controller => "phones", :action => "create" ) do %>
<%= hidden_field :original_number, params[:original_number] %>
<%= hidden_field :name, params[:name] %>
<%= hidden_field(:twilio_number, number.phone_number) %>
<div class="found_list">
<div class="found_phone_number">
<%= label_tag(:number, number.friendly_name) %>
</div>
<div class="choose_found_number">
<%= submit_tag("Choose This Number", :class => "btn btn-large btn-success") %>
</div>
</div>
<hr>
<% end %>
<% end %>
And, here’s the create action of the phones controller
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.nil?
flash[:success] = "Phone Number Created!"
redirect_to user_path
else
render new_find_number_path
flash[:error] = "It looks like there were errors with the submission"
end
end
What would I need to edit on the create action, to make it so it takes the parameters that the show action POSTS to it, and than saves it to the database?
I think this might be the culprit :
@phone = @user.phones.build(params[:phone])
Thanks for sticking with me this far! Any guidance would be greatly appreciated
Here’s the debugger information when I hit Phones#create via find_numbers/show
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: RMvjP9PQt9hVEJE449pMqgqnFcreqvXtmZFEyU+641g=
original_number: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
'1231231234': ''
name: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
Example Name: ''
twilio_number: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
'+18322415354': ''
commit: Choose This Number
action: create
controller: phones
The Phone Model
# == Schema Information
#
# Table name: phones
#
# id :integer not null, primary key
# name :string(255)
# twilio_number :string(255)
# original_number :string(255)
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Phone < ActiveRecord::Base
attr_accessible :name, :original_number, :twilio_number
belongs_to :user
validates :name, presence: true
validates :twilio_number, presence: true
validates :original_number, presence: true
validates :user_id, presence: true
default_scope order: 'phones.created_at DESC'
end
Try this