2016-07-27 4 views
0

私はWebサービスとRailsも初めてです。私はレール5を使用してAPIを作成することに疑念があります。どのようにレール5アプリケーション用のAPIを作成しますか? APIのみのアプリケーションでは、レール5を使用したチュートリアルがあります。しかし、APIとビューの両方が1つのレール5アプリケーションに必要です。私はどうしたらいいですか?レール5にAPIを作成する方法は?

+3

は '新しいAPIだけでアプリケーションを作成する新しいmy_api --api'コマンドをレール。あなたはapiとビューが必要な場合は、通常のアプリケーション(APIモードなし)を作成し、コントローラメソッド内のjsons/viewsをレンダリングします – AndreyS

+1

あなたはちょっと混乱しています。 Api専用モードは、特にビューを使用せず、使用されていないコードをロードするオーバーヘッドを必要としない、APIのみのアプリケーション向けです。あなたは完全に通常のレールアプリでAPIのエンドポイントを作成することができます。基本的に、apiエンドポイントはhtmlの代わりにjsonをレンダリングするコントローラーアクションです。それでおしまい。 –

+0

@SergioTulentsevありがとうございます。しかし、APIのルートをどのように設定する必要がありますか? – poombavai

答えて

0

あなたはいつものように新しいレールプロジェクトを作成することができます

$ rails new my_project 
$ cd my_project 
$ bundle 

をそしてあなたには、いくつかのコードを生成するためにscaffoldを使用することができます。

$ rails g scaffold Product name:string price:float 

をし、データベースを移行:

$ rails db:migrate # => update the database 

今すぐご覧になれますapp/controllers/products_controller.rb

class ProductsController < ApplicationController 
    before_action :set_product, only: [:show, :edit, :update, :destroy] 

    # GET /products 
    # GET /products.json 
    def index 
    @products = Product.all 
    end 

    # GET /products/1 
    # GET /products/1.json 
    def show 
    end 

    # GET /products/new 
    def new 
    @product = Product.new 
    end 

    # GET /products/1/edit 
    def edit 
    end 

    # POST /products 
    # POST /products.json 
    def create 
    @product = Product.new(product_params) 

    respond_to do |format| 
     if @product.save 
     format.html { redirect_to @product, notice: 'Product was successfully created.' } 
     format.json { render :show, status: :created, location: @product } 
     else 
     format.html { render :new } 
     format.json { render json: @product.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /products/1 
    # PATCH/PUT /products/1.json 
    def update 
    respond_to do |format| 
     if @product.update(product_params) 
     format.html { redirect_to @product, notice: 'Product was successfully updated.' } 
     format.json { render :show, status: :ok, location: @product } 
     else 
     format.html { render :edit } 
     format.json { render json: @product.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /products/1 
    # DELETE /products/1.json 
    def destroy 
    @product.destroy 
    respond_to do |format| 
     format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_product 
     @product = Product.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def product_params 
     params.require(:product).permit(:name, :price) 
    end 
end 

ご覧のとおり、createのアクションにはrespond_toが表示されています。これは、さまざまなリクエストタイプに対応する方法です。

それについての詳細を読むhttp://edgeapi.rubyonrails.org/classes/ActionController/MimeResponds.html#method-i-respond_to

+0

ありがとう@RaVeN。しかし、APIのルートをどのように設定すればよいですか? – poombavai

+0

'config/routes.rb'を見てください。それらは自動的に生成されます。 exapmle: '/ products/1.json'(あなたは毎回上記のコメントを見ることができます)を、' respond_to.json'を起動するために使用するのと同じルートを使用することができます。行動する方法) – siegy22

+0

ありがとう@RaVeN。 Railsは素晴らしいです。わかった。 – poombavai

関連する問題