获取Twilio 404错误Rails接收SMS

时间:2013-05-25 01:21:57

标签: ruby-on-rails ruby twilio

我正在尝试创建一个通过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函数有关。我做错了什么?

1 个答案:

答案 0 :(得分:1)

网址应为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.erb重命名为app/views/twilio/create.xml.erb

要创建新交易,请向post发出tooler.herokuapp.com/twilio.xml请求。该网址会点击def create中的TwilioController并呈现app/views/twilio/create.xml.erb

如果由于404错误仍无效,您可以查看rake routes以查看符合config/routes.rb的所有网址。