2

我正在尝试创建一个通过 Twilio 接受 SMS 消息的应用程序,然后创建一个与员工模型和项目模型相关的签入/签出事务。一个简单的基于 SMS 的项目结帐/签入跟踪器。我已经连接了 twilio 应用程序以在 tooler.herokuapp.com/twilio/twilio_create 上收听,但是当我向该号码发送消息时,没有任何反应,并且我在 twilio 的日志中收到 404 错误。不知道具体是怎么回事,希望有人能帮忙。在这种情况下,我从 twilio 获取 FROM 并将其放入 employee_id,从 twilio 获取 BODY 并将其放入 item_id。为什么它不会创建新的交易?

数据库/schema.rb

    ActiveRecord::Schema.define(:version => 20130516162824) do

      create_table "employees", :force => true do |t|
        t.string   "phone"
        t.string   "name"
        t.datetime "created_at", :null => false
        t.datetime "updated_at", :null => false
      end

      create_table "items", :force => true do |t|
        t.string   "description"
        t.string   "assettag"
        t.datetime "created_at",  :null => false
        t.datetime "updated_at",  :null => false
      end

      create_table "transactions", :force => true do |t|
        t.boolean  "status"
        t.integer  "item_id"
        t.integer  "employee_id"
        t.datetime "created_at",  :null => false
        t.datetime "updated_at",  :null => false
      end

      add_index "transactions", ["employee_id"], :name => "index_transactions_on_employee_id"
      add_index "transactions", ["item_id"], :name => "index_transactions_on_item_id"

      create_table "users", :force => true do |t|
        t.string   "email",                  :default => "", :null => false
        t.string   "encrypted_password",     :default => "", :null => false
        t.string   "reset_password_token"
        t.datetime "reset_password_sent_at"
        t.datetime "remember_created_at"
        t.integer  "sign_in_count",          :default => 0
        t.datetime "current_sign_in_at"
        t.datetime "last_sign_in_at"
        t.string   "current_sign_in_ip"
        t.string   "last_sign_in_ip"
        t.datetime "created_at",                             :null => false
        t.datetime "updated_at",                             :null => false
      end

      add_index "users", ["email"], :name => "index_users_on_email", :unique => true
      add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

    end

应用程序/控制器/twilio_controller.rb

    class TwilioController < ApplicationController

      def process_sms
        @city = params[:FromCity].capitalize
        @state = params[:FromState]
          render 'process_sms.xml.erb', :content_type => 'text/xml'
        end

      def twilio_create
        @transaction = Transaction.new(:item_id => params[:Body], :employee_id => params[:From])
        @transaction.save
      end

    end

应用程序/视图/twilio/twilio_create.xml.erb

<Response>                                                                                                                 
  <Sms>Received. You checked out <%= @body %>, <%= @from %> you lucky bastard.</Sms>
</Response>

我已经让它与 process_sms 页面一起工作,所以我知道它与 twilio_create 函数有关。我究竟做错了什么?

4

1 回答 1

1

URL 应该是tooler.herokuapp.com/twilio/twilio_create.xml? 您可以rake routes查看所有符合您的config/routes.rb.

实际上,Rails 已经有了 CRUD 约定。由于您正在创建 twilio 资源,因此您config/routes.rb应该:

# config/routes.rb
resources :twilio do
  collection do
    get :process_sms
  end
end

在控制器中,您应该使用def create而不是def twilio_create.

class TwilioController < ApplicationController

  def process_sms
    @city = params[:FromCity].capitalize
    @state = params[:FromState]
      render 'process_sms.xml.erb', :content_type => 'text/xml'
    end

  def create
    @transaction = Transaction.new(:item_id => params[:Body], :employee_id => params[:From])
    @transaction.save
  end

end

最后,重命名app/views/twilio/twilio_create.xml.erbapp/views/twilio/create.xml.erb.

为了创建一个新事务,post请向tooler.herokuapp.com/twilio.xml. 该 URL 将进入def createTwilioController呈现app/views/twilio/create.xml.erb

如果由于 404 错误仍然无法正常工作,您可以检查rake routes以查看所有符合您的config/routes.rb.

于 2013-05-25T02:34:42.433 回答