2016-03-30 13 views
2

Phoenixを使用してEctoまたはブランチを使用せずにREST APIを作成しようとしています。フェニックスのパラームで投稿する(エクトはありません)?

Ectoを使用せず、パラメータを使用してルータ/コントローラにポスト機能を作成する構文は何ですか?

は、Ruby/Sinatraで例えば、それはこのようなものになります。

post "/v1/ipf" do 
    @weight1 = params[:weight1] 
    @weight2 = params[:weight2] 
    @weight3 = params[:weight3] 
    @weight4 = params[:weight4] 
    @goal1_percent = params[:goal1_percent] 
    @goal2_percent = params[:goal2_percent] 

    # etc... 
end 

アップデートを

ニックの回答に基づいて、ここで私がなってしまったものです:

rest_api/web/router.ex:

defmodule RestApi.Router do 
    use RestApi.Web, :router 

    pipeline :api do 
    plug :accepts, ["json"] 
    end 

    scope "/", RestApi do 
    pipe_through :api 

    scope "/v1", V1, as: :v1 do 
     get "/ipf", IPFController, :ipf 
    end 
    end 

end 

rest_api /ウェブ/コントローラ/ V1/ipf_controller.ex:

defmodule RestApi.V1.IPFController do 
    use RestApi.Web, :controller 

    import IPF 

    def ipf(conn, params) do 
    {weight1, _} = Integer.parse(params["weight1"]) 
    {weight2, _} = Integer.parse(params["weight2"]) 
    {weight3, _} = Integer.parse(params["weight3"]) 
    {weight4, _} = Integer.parse(params["weight4"]) 
    {goal1_percent, _} = Float.parse(params["goal1_percent"]) 
    {goal2_percent, _} = Float.parse(params["goal2_percent"]) 

    results = IPF.ipf(weight1, weight2, weight3, weight4, goal1_percent, goal2_percent) 

    render conn, results: results 
    end 

end 

rest_api /ウェブ/ビュー/ V1/ipf_view.ex:

defmodule RestApi.V1.IPFView do 
    use RestApi.Web, :view 

    def render("ipf.json", %{results: results}) do 
    results 
    end 

end 

答えて

4

エクトとブランチは本当に持っていませんPhoenixがPOSTを処理する場合は何もしません。 BrunchはWebアセット構築ツールであり、Ectoはデータベース層です。この新しいルートを追加するには

、あなただけの新しいルートのためのルータにエントリを追加する必要があります。

defmodule MyModule.SPFController do 
    def spf(conn, params) do 
    # do whatever 
    end 
end 

それだ:コントローラを作成、その後

post "/v1/spf", SPFController, :spf 

と。

+1

ありがとうございます!私が見つけることができるすべてのPheonix/RESTの例は、エクトベースのCRUDの例だったので、私が「エコとブランチはない」という唯一の理由があります。 –

関連する問題