I’m new to Rails and Android, so please don’t blame me if the question was answered before somewhere, but after searching for an answer I did’t find anything.
Question: How do I assure that Rails creates or updates the field IP which should also be unique in the database. I sent a HTTP POST from Android device to “http://192.168.5.3:3000/users/create?user%5Buser_ip%5D=”+myIp+”user%5Buser_name%5D=”+name
So on the Rails’ server side, Rails should check if myIp already exists and if so, it should update the “user_name” to “name”, and if it won’t find myIp, it should create a new record with both fields.
Should I use before_create in the user.rb model? Or how do I do that? Should I use POST or PUT? If I should use PUT, then I don’t know the id if the user exists.
Or shouldn’t I use the Rails action “create” at all and write a custom Webservice method?
Sorry for such maybe lame question but somehow I can’t figure it out!
Thanks for help in advance!
You want to use PUT for update and POST for create. If you know the user exists, but you don’t have the id, you would use a PUT request, and in the update method, you would have something like this:
Again, the ideal method would be to pass the id into the update, but if you are unable to do that, you don’t want to sacrifice RESTful conventions.