【问题标题】:Change the value of a model's attribute更改模型属性的值
【发布时间】:2012-06-27 21:29:55
【问题描述】:

我正在使用 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

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

提前致谢!

【问题讨论】:

  • 请发布您的查看代码。正如我在 Heroku 服务器日志中看到的那样,您的参数看起来像 Parameters: {"AccountSid"=&gt;"AC0626c6d8dc0a4551b159161c5ca7ced2", "Body"=&gt;"4", "ToZip"=&gt;"13203", "FromState"=&gt;"NY", "ToCity"=&gt;"SYRACUSE", "SmsSid"=&gt;"SMa973490ac1db98b25247b600fe068d6f", "ToState"=&gt;"NY", "To"=&gt;"+13158491369", "ToCountry"=&gt;"US", "FromCountry"=&gt;"US", "SmsMessageSid"=&gt;"SMa973490ac1db98b25247b600fe068d6f", "ApiVersion"=&gt;"2010-04-01", "FromCity"=&gt;"SYRACUSE", "SmsStatus"=&gt;"received", "From"=&gt;"+13154368655", "FromZip"=&gt;"13135"} 但它应该看起来像 Parameters: {"user" =&gt; {"AccountSid" =&gt; ..}}
  • 贴出查看代码——感谢您的帮助:
  • 嗨,我建议逐步检查您的逻辑,并在每种情况下检查您的假设。你真的检索用户对象吗?您是否有“发件人”号码? user.save 是否更新存储在数据库中的值?这些都是代码可能被破坏的地方,最好检查每个地方。

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


【解决方案1】:

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

protect_from_forgery

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

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

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

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2013-03-15
    • 2013-11-08
    • 2023-03-11
    • 1970-01-01
    • 2016-09-13
    相关资源
    最近更新 更多