来自Android调用的参数不能通过params hash获得

时间:2017-02-28 01:28:33

标签: android ruby-on-rails

通常,当我从Web应用程序进行POST或GET HTTP调用时,哈希可直接从params哈希中获得:

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

但是,我已经从Android应用程序发送了内容类型'application / json',如下所示:

JSONObject holder = new JSONObject();
JSONObject userObj = new JSONObject();
userObj.put("username", params.get("name"));
userObj.put("uid", params.get("uid"));
userObj.put("image", params.get("image"));
userObj.put("provider", params.get("provider"));
holder.put("user", userObj);
query = holder.toString();


url = new URL("http://10.0.2.2:3001/users/auth/facebook/callback.json ");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
OutputStream os = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);

当我查看日志时,数据格式很好:

Processing by OmniauthCallbacksController#facebook as JSON
Parameters: {"user"=>{"username"=>"John Dough", "uid"=>"11111222444", ...  

但是,我无法访问params哈希:

params_user # => nil

def params_user
  params[:user].permit!
end

为什么我不能在明显可用时访问params哈希?

如果我尝试像这样访问它:

params.parameters['user']

它告诉我params的参数方法是受保护的。但是,当我使用调试器时,这是我看到数据的唯一地方。

NoMethodError (protected method `parameters' called for #<ActionController::Parameters:0x007fc51d1f9e08>):

1 个答案:

答案 0 :(得分:2)

不确定您使用的是哪个版本的Rails,但在现代Rails中,您可以这样做:

ch.new <- t(apply(ch, 1, function(row){row[which.max(cumsum(row))] <- "max"; ifelse(row=="max", 1, 0)}))
ch.new[1:6,]
   occasions
id  1979-06-30 1979-12-29 1980-06-29 1980-12-28 1981-06-29 1981-12-28 1982-06-29 1982-12-28 1983-06-29 1983-12-28 1984-06-28
  1          0          0          0          0          0          0          0          0          0          0          0
  2          0          0          0          0          0          0          0          0          0          0          0
  4          0          0          0          0          0          0          0          0          0          1          0
  5          0          0          0          0          0          0          0          0          0          0          0
  6          0          0          0          1          0          0          0          0          0          0          0
  8          0          0          0          1          0          0          0          0          0          0          0
   occasions
id  1984-12-27 1985-06-28 1985-12-27 1986-06-28 1986-12-27 1987-06-28 1987-12-27 1988-06-27 1988-12-26 1989-06-27 1989-12-26
  1          0          0          0          0          0          0          0          0          0          0          0
  2          0          0          0          0          0          0          1          0          0          0          0
  4          0          0          0          0          0          0          0          0          0          0          0
  5          0          0          0          0          0          0          0          0          0          0          0
  6          0          0          0          0          0          0          0          0          0          0          0
  8          0          0          0          0          0          0          0          0          0          0          0
   occasions
id  1990-06-27 1990-12-26 1991-06-27 1991-12-26 1992-06-26 1992-12-25 1993-06-26
  1          0          0          1          0          0          0          0
  2          0          0          0          0          0          0          0
  4          0          0          0          0          0          0          0
  5          0          0          0          0          0          1          0
  6          0          0          0          0          0          0          0
  8          0          0          0          0          0          0          0