I have method create in my users controller for creating users for my web. But if a user wants to create an account from his iOS, should be there be two different methods defined on the userscontroller, one for the web and other for the mobile? What should be the names of those methods, and then In the iOS case I will have to remove storing cookies and remove the signin on signup scenario.
I just want him to be able to create an account. How can I do this?
Currently I have defined a new method called create_from_mobile with a route
I recommend you make another controller, under an api that your mobile app will use. It would be in
app/controllers/api/users_controller.rb, and it would still be the create action of that controller.
The route for that would be like this
namespace :api do
resources :users, only: [:create]
This this api controller can just accept and respond with json, or whatever the mobile app expects for working with.
thanks for the question, please let me know if thats not clear or doesn’t work for you,
I have done the changes, added your changes and inside my create method doing a respond_to @user in case the save is successful. But should be returned if the save is unsuccessful?
I see a suggestion of doing respond_with(project, :location => api_v1_project_path(project)) on stackoverflow but just wanted to know what your return value be?
Also then should the url from the mobile be http://idlecampus.com/api/users and the http method be post? Or should it be http://idlecampus.com/api/users.json if I want to return back a json?
I have added respond_with :json inside the controller and do a respond_to(@user) inside the action. What will happen If I were to do a respond_to :html,:json inside the controller and have a respond_to(@user) inside the method? Does the url matter if respond_with :html,:json instead just respond_with :json.
@ankur_kothari if you are build a json api for your iOS app than you could follow @cpytel suggestion.
In order to make all the api requests in your api designated controllers to respond with json you could do something like this:
class Api::V2::BaseController < ActionController::Base
In your base controller. Then you’d just need subclass it.
And your research on stackoverflow gave you a good advice. A rule of thumb for post/create request with json api is to return the object you just created for several reasons: 1) even though you might’ve sent all the object data from the ios app the server still might need to calculate some other data that the ios app can/should use(like remote_id/id or token, etc.). 2) you don’t need to send another GET request to check the new object’s data. 3) just a safety “double-check” again, you might’ve had all the data on the client side but it wouldn’t hurt to compare it with what you get from the server in return.
Hope it helps. Let me know if you need more details on this.