更改模型属性的值

时间:2012-06-27 21:29:55

标签: ruby-on-rails rest heroku attributes twilio

我正在使用Twilio发送短信来更改特定型号(用户)属性的值(在这种情况下:钱)。

我成功地从Twilio POST中删除了params,虽然没有运气将属性更改保存到模型的属性中。有什么想法吗?

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

  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

最后,这是收到短信的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>

提前致谢!

1 个答案:

答案 0 :(得分:1)

日志显示WARNING: Can't verify CSRF token authenticity,因此,由于您的rails应用程序端的身份验证失败,您可能无法从'twilio'获取数据。喜欢

protect_from_forgery

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

尝试除了伪造保护之外的行为,并查看类似

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

HTH