Donato Donato -4 years ago 90
Android Question

Parameters from Android call not available through params hash

Typically, when I make a POST or GET HTTP call from a web app, the hash is available directly from the params hash:

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


However, I have sent content type 'application/json' from an Android app, as so:

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);


And when I looked at the logs, the data is nicely formatted:

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


However, I cannot access the params hash as so:

params_user # => nil

def params_user
params[:user].permit!
end


Why can I not access the params hash when it is clearly available?

If I try to access it like this:

params.parameters['user']


It tells me the parameters method of params is protected. But that is the only place I see my data when I use the debugger.

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

Answer Source

not sure which version of Rails you're using but in modern Rails you do this:

def params_user
  params.require(:user).permit(:email, :name, :other_attributes)
end
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download