1

在从树莓派解析和检索放置请求时遇到一些问题,我已经发送了我的网站数据。

我正在尝试更新我的 Worker 模型中的一些字段,控制器在下面

class WorkersController < ApplicationController

def new
   @worker = Worker.new
   @user = User.find(params[:user_id])
end

def create
   @user = User.find(params[:user_id])
   @worker= Worker.new(params[:worker])
   @worker.user_id= @user.id
   if @worker.save
    flash[:success] = "Worker information saved!"
    redirect_to @user
   else
     render 'new'
   end
end


def show
end

def update
    @user = User.find(params[:user_id])
    @worker = Worker.find(params[:id])
    @MHS = params[:SUMMARY][:MHS_av]
    @worker.hashrate = @MHS
    @worker.save
end


end

下面是服务器端传入的put请求

Started PUT "/users/1/workers/1" for xxx.xxx.xx.xxx at 2013-06-13 03:06:02 +0000
Processing by WorkersController#update as XML

现在这里是参数

Parameters: {"STATUS"=>[{"STATUS"=>"S", "When"=>1371092750, "Code"=>11, "Msg"=>"Summary", "Description"=>"cgminer 3.1.1"}], "SUMMARY"=>[{"Elapsed"=>60394, "MHS av"=>1.92, "Found Blocks"=>1, "Getworks"=>1217, "Accepted"=>9104, "Rejected"=>138, "Hardware Errors"=>0, "Utility"=>9.04, "Discarded"=>2428, "Stale"=>20, "Get Failures"=>1, "Local Work"=>9513, "Remote Failures"=>0, "Network Blocks"=>458, "Total MH"=>116212.6295, "Work Utility"=>1769.84, "Difficulty Accepted"=>1747840.0, "Difficulty Rejected"=>26496.0, "Difficulty Stale"=>3840.0, "Best Share"=>74960510}], "id"=>"1", "user_id"=>"1", "worker"=>{}}

其次是这些错误

WARNING: Can't verify CSRF token authenticity

at=info method=PUT path=/users/1/workers/1 host=miningmonitor.herokuapp.com fwd="xxx.xxx.xx.xxx" dyno=web.1 connect=1ms service=1029ms status=500 bytes=643

TypeError (can't convert Symbol into Integer):


app/controllers/workers_controller.rb:27:in `[]'

Completed 500 Internal Server Error in 345ms



app/controllers/workers_controller.rb:27:in `update'

我看到 Ruby on rails 认为传入的消息是 XML,但它的 JSON 我需要在某处指定这个 put 请求是 JSON 吗?另外有人告诉我,Ruby on rails 只是将 JSON 解析为参数,我可以从参数中提取数据,这是真的吗?

从布局中添加来自 application.html.erb 的内容

<head>
 <title>Mining Monitoring Website</title>
 <%= stylesheet_link_tag "application", media: "all"%>
 <%= javascript_include_tag "application" %>
 <%= csrf_meta_tags %>
 <%= render 'layouts/shim' %>
</head>
4

1 回答 1

0

仔细看看——SUMMARY 是一个数组,而不是散列。我不知道您是如何构建 POST (PUT) 数据的,但是按照以下方式访问它们应该可以

params[:SUMMARY][0][:MHS_av]

另外 - 请看一下MHS_av- 在传入参数中它看起来没有下划线。

CSRF 失踪是有原因的,对吧?

于 2013-06-13T05:31:33.703 回答