【问题标题】:Getting a Twilio 404 Error Rails Receiving SMS获取 Twilio 404 错误 Rails 接收 SMS
【发布时间】:2013-05-25 01:21:57
【问题描述】:

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

db/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

app/controllers/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

app/views/twilio/twilio_create.xml.erb

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

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

【问题讨论】:

    标签: ruby-on-rails ruby twilio


    【解决方案1】:

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

    实际上,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

    要创建新事务,请向tooler.herokuapp.com/twilio.xml 发出post 请求。该 URL 将命中 TwilioController 中的 def create 并呈现 app/views/twilio/create.xml.erb

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

    【讨论】:

    • 试过了...这就是我得到的结果:错误 HTTP 检索失败返回了 HTTP 状态代码 500。AccountSid AC403799e6989feb71259aff925d6d6b17 ApiVersion 2010-04-01 Body Huh From +17177564437 FromCity HARRISBURG FromCountry US FromState PA FromZip 17307 SMESSISISID SMAA17FBE4CC6A1AFB29BFF8C5921E4867 SMSSID SMAA17FBE4CC6A1AFB29BFF8C5921E4867 SMSSTATUS收到至+17177395006 TOCITY SPRING GROVE TOCOUDTRY US TOSTATE PA TOZIP 17404 我们很抱歉(500) title>
    • 来自调试器部分 Twilio 的日志。尝试了您推荐的方法,但没有成功。 Rake 路由看起来不错,我有一个 /twilio 的 POST 条目,它以 twilio#create 结尾。以前是 404,现在是 500。
    • 现在我无法通过 Web 界面创建正常的交易 - 我搭建了交易的原始模型,只是为了确保我得到正确的关系......现在当我尝试创建一个手动通过网络应用程序出错。
    • 我很笨。我将电话号码作为员工 ID 传递,但没有匹配的员工 ID。这就是它出错的原因。谢谢你的帮助,我现在明白了。
    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2018-06-29
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多