0

现在我正在尝试构建一个能够搜索电话号码、购买电话号码并将交易记录到数据库的应用程序。

我想这样做的方法是有两个控制器——(1) find_numbers 控制器接受参数并搜索数字。(2) 手机控制器购买选择的号码并将参数保存到数据库中。

这是我正在尝试做的图表。

http://www.stepanp.com/search-and-buy.jpg

虽然我可以搜索号码并购买号码,但我似乎无法将其保存到数据库中。

它重定向我并告诉我我没有填写正确的字段。

这让我认为 find_numbers 表单中的参数不被接受为手机控制器创建操作的参数

这是 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 %>

而且,这里是手机控制器的创建动作

  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

我需要在 create 操作上编辑什么,使其采用 show 操作 POSTS 的参数,然后将其保存到数据库中?

我认为这可能是罪魁祸首:

@phone = @user.phones.build(params[:phone])

谢谢你一直陪着我!任何指导将不胜感激

这是我通过 find_numbers/show 点击 Phones#create 时的调试器信息

--- !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

电话模型

# == 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

http://stepanp.com/search-and-buy-debug.jpg

4

1 回答 1

0

试试这个

def create
    @user = current_user
    @phone = @user.phones.new(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.present?
      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
于 2012-10-04T14:45:51.223 回答