2017-06-04 87 views
0

我试图实现一个控制器方法,其中一个帐户向另一个帐户发送一笔钱。在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来测试我的后端的功效,因为我所在的程序并没有教会我们如何使用rails视图。我怎样才能写出一个curl请求来测试我的方法?

编辑:我的完整控制器代码。这是用脚手架生成的,并且被稍微修改

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是一个浮动,用于修改捐款方法帐户和目标帐户的余额属性,而客户端是捐款目标帐户的id属性。正如你所看到的,我的curl请求尚未完成,因为我不知道如何将值传递给方法参数。 – d00medman

回答

0

以下是工作方法的代码和卷曲请求N3我的控制器:

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 

和我(令人发指的硬编码)卷曲要求:

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'对象,或者你正在使用脚手架的方法'set_amt'? –

+0

'amt'不是帐户的财产,它是支付人和收款人帐户余额将被修改的金额。 – d00medman

相关问题