2016-07-27 60 views
0

我是新來的Web服務和Rails了。我有使用rails 5創建API的疑問。如何爲rails 5應用程序創建API?我可以在API中找到一些教程使用rails 5的應用程序。但是我需要在單個rails 5應用程序中使用API​​和視圖。我應該怎麼做?如何在rails 5中創建API?

+3

'軌新my_api --api'命令來創建新的API只應用。如果你需要api和視圖,創建一個普通的應用程序(不用api模式),然後在你的控制器方法中渲染jsons/views – AndreyS

+1

你有點困惑。僅限於Api的模式專門用於api-only應用程序,它不使用視圖,也不需要加載未使用代碼的開銷。你完全可以在普通的rails應用程序中創建api端點。基本上,一個API端點是一個控制器動作,呈現json而不是html。而已。 –

+0

謝謝@SergioTulentsev。但我應該如何配置API的路線? – poombavai

回答

0

您可以創建一個新的Rails項目和往常一樣:

$ 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行動。這是您可以如何響應不同的請求類型。

瞭解更多關於它:http://edgeapi.rubyonrails.org/classes/ActionController/MimeResponds.html#method-i-respond_to

+0

謝謝@RaVeN。但我應該如何配置我的路線的API? – poombavai

+0

看看'config/routes.rb'。它們會自動生成。您可以使用與檢索html相同的路由,以觸發'respond_to.json',您可以將'.json'附加到路由:exapmle:'/ products/1.json'(您可以看到上面的註釋行動如何達到他們) – siegy22

+0

Thankyou @RaVeN。 Rails很棒。我知道了。 – poombavai