Creating a user with the Rails API and JSON?

So, I am testing a very simple API in Rails to find out if I can create a user from it locally using the Google Postman plugin (REST client extension).

In my rails application, I created a folder / namespace for my API, and whenever I try to create my user, I get the following error: There is no api / v1 / users / create, application / create template with {: locale => [ : en] ,: formats => [: json] ,: handlers => [: erb ,: builder ,: raw ,: ruby ​​,: jbuilder ,: coffee]}. Search: * "PATH / app / views"

I am using Rails 4.0.1 and Ruby 2.0

I am posting a screenshot below what I am posting:

Screen shot 1

module Api module V1 class UsersController < ApplicationController class User < ::User # add any hacks end respond_to :json def index respond_with User.all end def show respond_with User.find(params[:id]) end def new @user = User.new end def create @user = User.create(user_params) # respond_with(@user) if @user.save # render json: @user, status: :created, location: @user redirect_to @user end end private def user_params params.require(:user).permit(:name, :age, :location) if params[:user] end end end end 

So based on my user_params, I should be able to create a new user, right? Please let me know if you need more information and I will do my best to reply as soon as possible! Thanks!

+7
json api ruby-on-rails ruby-on-rails-4
source share
2 answers

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 # ... if needed end end root to: 'users#index' end 

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 # ... respond_to do |format| format.html {render text: "Your data was sucessfully loaded. Thanks"} format.json { User.create(... params ...) render text: User.last.to_json # ! } end end 

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 
+22
source share

redirect dose is not returned, so your create method will continue to search for a template to render, and then find the appropriate template.

To fix, you need to explicitly return redirect

  if @user.save return redirect_to(@user) end 

You also need to pay attention to the default @user URL. It is better to assign a named path explicitly in this case, say redirect_to(user_path(@user))

0
source share

All Articles