You can create a user using the API.
1) First you need to put the right resources on your routes. rb:
YourApp::Application.routes.draw do namespace :api do namespace :v1 do resources :users end namespace :v2 do
2) To process requests, you need to create a RESTfull style controller. Here you can implement your create action.
def create respond_with User.create(fio: params[:fio], phone: params[:phone], region: params[:region], updated_at: Time.now) end
Example "create" with reply_to:
def create
See the docs respond_with and respond_to if you need anything special to answer.
Railscasts API build episodes may also be helpful: # 350 and # 352
PS folder / namespace / v 1 / users_controller should be the same as the class name in your Api module
PS2 You can watch my application where you can find something useful (just like your application is a simple API for creating posts) - myApp
Users_controller example (controllers / api / v1 / users_controller.rb):
#encoding: utf-8 module Api module V1 class UsersController < ApplicationController # Api::BaseController before_filter :authenticate_user!, except: [:create, :index] respond_to :json def index #respond_with respond_to do |format| format.html {render text: "Your data was sucessfully loaded. Thanks"} format.json { render text: User.last.to_json } end end def show respond_with User.find(params[:id]) end def create respond_with User.create(access_token: params[:access_token], city: params[:city], created_at: Time.now, phone: params[:phone], region: params[:region], updated_at: Time.now) end def update respond_with User.update(params[:id], params[:users]) end def destroy respond_with User.destroy(params[:id]) end end end end
Mihail Davydenkov
source share