0

我正在使用 Twilio 发送 SMS 消息以更改特定模型(用户)属性的值(在本例中为:money)。

我成功地从 Twilio POST 中提取了参数,尽管没有运气将属性更改保存到模型的属性中。有什么想法吗?

这是用户控制器中“更新”的代码:

  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }

      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end

    require 'rubygems'
    require 'twilio-ruby'

    @account_sid = 'AC0626c6d8dc0a4551b159161c5ca7ced2'
    @auth_token = '**********************************'

    # set up a client to talk to the Twilio REST API
    @client = Twilio::REST::Client.new(@account_sid, @auth_token)

    message_body = params["Body"]
    from_number = params["From"]

    if from_number == "+**********"
      @user = User.find(1) # :select => :money
      existing_money = @user.money
      doug_sum = message_body.to_f + existing_money

      @user.money = doug_sum
      @user.save

      puts "This is Doug's sum: #{doug_sum} and it has been saved"
    else
      puts "This is not a valid frump number"  
    end

  end

以下是 Heroku 服务器日志的内容:

2012-06-27T21:09:06+00:00 app[web.1]: Started POST "/users" for 184.73.31.10 at 2012-06-27 21:09:06 +0000
2012-06-27T21:09:06+00:00 app[web.1]: Processing by UsersController#create as HTML
2012-06-27T21:09:06+00:00 app[web.1]:   Parameters: {"AccountSid"=>"AC0626c6d8dc0a4551b159161c5ca7ced2", "Body"=>"4", "ToZip"=>"13203", "FromState"=>"NY", "ToCity"=>"SYRACUSE", "SmsSid"=>"SMa973490ac1db98b25247b600fe068d6f", "ToState"=>"NY", "To"=>"+13158491369", "ToCountry"=>"US", "FromCountry"=>"US", "SmsMessageSid"=>"SMa973490ac1db98b25247b600fe068d6f", "ApiVersion"=>"2010-04-01", "FromCity"=>"SYRACUSE", "SmsStatus"=>"received", "From"=>"+13154368655", "FromZip"=>"13135"}
2012-06-27T21:09:06+00:00 app[web.1]: WARNING: Can't verify CSRF token authenticity
2012-06-27T21:09:06+00:00 app[web.1]:   Rendered users/_form.html.erb (5.4ms)
2012-06-27T21:09:06+00:00 app[web.1]:   Rendered users/new.html.erb within layouts/application (34.9ms)
2012-06-27T21:09:06+00:00 app[web.1]: Completed 200 OK in 47ms (Views: 36.7ms | ActiveRecord: 1.5ms)
2012-06-27T21:09:06+00:00 heroku[router]: POST frump.herokuapp.com/users dyno=web.1 queue=0 wait=0ms service=57ms status=200 bytes=1582

最后,这是接收 SMS 的 Twiml 文件:

<?xml version="1.0" encoding="UTF-8" ?> 
<Response>
    <Redirect method="POST">http://frump.herokuapp.com/users</Redirect>
</Response>

这是视图代码:

<% @users.each do |user| %>
  <tr>
    <td><%= user.name %></td>
    <td>$<%= user.money %></td>
    <td><%= link_to 'Show', user %></td>
    <td><%= link_to 'Edit', edit_user_path(user) %></td>
    <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

提前致谢!

4

1 回答 1

1

日志说WARNING: Can't verify CSRF token authenticity,所以可能由于您的 Rails 应用程序端的身份验证失败,您没有从“twilio”获取数据。就像拥有

protect_from_forgery

在您的应用程序控制器中。

尝试从伪造保护中排除您的行为,然后查看,例如

class UsersController < ApplicationController
  protect_from_forgery :except => ["create"]
end

高温高压

于 2012-06-28T05:32:48.787 回答