努力正确实现自定义rails控制器方法

时间:2017-06-04 23:03:07

标签: ruby-on-rails ruby

我正在尝试实施一种控制器方法,其中一个帐户向另一个帐户发送一笔钱。 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对象的before操作)

为此,我写了以下自定义路线:

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

我在实现此方法时遇到了一些问题,但最重要的是我不知道如何编写可以将值传递给amt和{{1}的cURL请求参数。我几乎完全使用cURL来测试我的后端的功效,因为我所在的程序没有教我们如何使用rails视图。如何编写curl请求来测试我的方法?

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

client

至于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

1 个答案:

答案 0 :(得分:0)

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

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