2017-06-04 7 views
0

1つのアカウントで別のアカウントに金額を送信するコントローラメソッドを実装しようとしています。 accnts_controller内のコードは次のとおりです。カスタムレールコントローラメソッドを正しく実装するための苦闘

def donate(amt, client) 
    if creator? 
     raise 'Only Patron accounts can donate' 
    else 
     @client = Accnt.find(client) 
     @client.balance += amt 
     @accnt.balance -= amt 
     @client.save 
     @accnt.save 
    end 
    end 

(注意:私のコントローラでは、私は@accntオブジェクトを設定し、アクションの前に持って)これを行うには

は、私は、次のカスタムルートを書かれています:

patch 'accnts/:id/donate' => 'accnts#donate' 

私はこの方法を実行して問題のカップルを持ってきたが、遠く離れた最大のものは、私がamtとに値を渡すことができcURLの要求を作成する方法を知らないということです引数。私が使っているプログラムでは、レールビューの使い方を教えてくれなかったので、バックエンドの有効性をテストするために、私はほとんど独占的にcURLを使用しています。メソッドをテストするためにカールリクエストを書くにはどうすればいいですか?

EDIT:私のフルコントローラコード。これは、足場で生成されたと少しカール要求については

class AccntsController < OpenReadController 
    before_action :set_accnt, only: %i[show update donate destroy] 
    # GET /accnts 
    def index 
    @accnts = Accnt.all 
    render json: @accnts 
    end 
    # GET /accnts/1 
    def show 
    render json: @accnt 
    end 
    # POST /accnts 
    def create 
    if current_user.accnt 
     raise 'Already has an account' 
    else 
     @accnt = Accnt.new(accnt_params) 
     @accnt.user = current_user 
     if @accnt.save 
     render json: @accnt, status: :created 
     else 
     render json: @accnt.errors, status: :unprocessable_entity 
     end 
     # render json: @accnt, status: :created, location: @accnt if @accnt.save 
    end 
    end 
    # PATCH/PUT /accnts/1 
    def update 
    if @accnt.update(accnt_params) 
     render json: @accnt 
    else 
     render json: @accnt.errors, status: :unprocessable_entity 
    end 
    end 
    # amt is the amount to be sent from the patron to the client, client is the client ID 
    def donate(amt, client) 
    # furthermore, I cannot say for certain whether this method of passing parameters is viable in rails 
    if creator? 
     raise 'Only Patron accounts can donate' 
    else 
     # Very easily could be logical errors here 
     @client = Accnt.find(client) 
     @client.balance += amt 
     @accnt.balance -= amt 
     @client.save 
     @accnt.save 
    end 
    end 
    # DELETE /accnts/1 
    def destroy 
    @accnt.destroy 
    end 
    private 
    # Use callbacks to share common setup or constraints between actions. 
    # To be used as a before method to determine whether or not the account in 
    # question is a creator account 
    def creator? 
     creator 
    end 
    def set_accnt 
     @accnt = Accnt.find(params[:id]) 
    end 
    # Only allow a trusted parameter "white list" through. 
    def accnt_params 
     params.require(:accnt).permit(:user_id, :user_name, :balance, :creator, :content_type, :content_list) 
    end 
end 

を変更されている、私は本当にこれ以上それを書いていない:

API="http://localhost:4741" 
URL_PATH="/accnts" 
curl "${API}${URL_PATH}/${ID}/donate" \ 
    --include \ 
    --request PATCH \ 
    --header "Content-Type: application/json" \ 
    --header "Authorization: Token token=$TOKEN" \ 
    --data 
echo 
+1

コントローラとcURLリクエストを追加できますか? –

+0

'amt'と' client'はどのように定義されていますか? (どのようなパラメータを使用していますか?)また、あなたの現在のcURL試行がどのように見えるかを示してください。そうでなければ、間違ったことを知る方法がありません。 –

+0

私は要求されたコードで自分の答えを更新しました。 @TomLord; amtは、donateメソッドを呼び出すアカウントとそれがターゲットとするアカウントの両方のbalanceプロパティを変更するfloatです。一方、clientは、寄付対象のアカウントのidプロパティです。ご覧のように、メソッドの引数に値を渡す方法がわからないので、私のカールのリクエストはまだ完了していません。 – d00medman

答えて

0

次は、加工方法コードであり、カール要求nは私のコントローラ:

def donate 
    if @accnt.creator 
    raise 'Only Patron accounts can donate' 
    else 
    client = params[:client_id] 
    amt = params[:amt] 
    # Very easily could be logical errors here 
    @client = Accnt.find(client) 
    @client.balance += amt 
    @accnt.balance -= amt 
    @client.save 
    @accnt.save 
    end 
end 

そして、私の(恐ろしくハードコード)cURLの要求:

API="http://localhost:4741" 
URL_PATH="/accnts" 
ID="2" 
TOKEN="BAhJIiU2ZDA2MGI4YzZkZjFkMmRkYTBkMWI3ZmRjMmRiMTZlYgY6BkVG--e55aee8e39c0cdd0bf944fa66661f06930037ba1" 
curl "${API}${URL_PATH}/${ID}/donate" \ 
    --include \ 
    --request PATCH \ 
    --header "Content-Type: application/json" \ 
    --header "Authorization: Token token=$TOKEN" \ 
    --data '{ 
    "client_id": 1, 
    "amt": 10.0 
    }' 
echo 
+0

ここで 'amt'を定義していますが、d00medmanを使用していません。 'amt'オブジェクトを' find'する必要はありませんか、足場のメソッド 'set_amt'を使っていますか? –

+0

'amt'は口座のプロパティではありません。これは、支払人と受取人の勘定の残高が修正される金額です。 – d00medman

関連する問題