控制器中是否有针对不同参数的单独更新操作?

时间:2018-11-21 06:08:34

标签: ruby-on-rails ruby

(帖子更新如下)

我的问题:我需要针对控制器中的不同参数执行单独的更新操作。我需要针对参数的常规更新操作(单独的更新操作/方法中的减去参数)、: video的更新操作以及:order_status

的更新操作

问题:如何有效地在控制器中完成多个更新操作?

这是我想要的东西,它不起作用:

  def charge_update
    respond_to do |format|
      @amount = (@order.order_price).to_i * 100
      @amount_seller = (@order.order_price).to_i * 75
      if @order.update(order_charge)
        if user_signed_in? && user.seller?
                      charge = Stripe::Charge.create({
                        :amount      => (@order.order_price).to_i * 100,
                        :description => 'Rails Stripe customer',
                        :currency    => 'usd',
                        :customer => @order.stripe_customer_token,
                        :destination => {
                          :amount => @amount_seller ,
                          :account => (@order.seller.stripe_token),
                        }
                      })
          @order.order_status = "charged"
          format.html { redirect_to ([@user, @order]), notice: 'Order was successfully uploaded.' }
          format.json { render :show, status: :ok, location: @order }
        else
          format.html { render :edit }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  def cancel_update
    respond_to do |format|
      if @order.cancel_update(order_update)
        if user_signed_in?
          if @order.order_status = "cancelled"

            format.html { redirect_to @order, notice: 'Order was successfully cancelled.' }
            format.json { render :show, status: :ok, location: @order }
          else
            format.html { render :edit }
            format.json { render json: @order.errors, status: :unprocessable_entity }
          end
        end
      end
    end
  end

  def update
    respond_to do |format|
      if @order.update( order_params.except(:order_status) )
        if user_signed_in?
          format.html { redirect_to ([@user, @order]), notice: 'Order was successfully updated.' }
          format.json { render :show, status: :ok, location: @order }
        else
          format.html { render :edit }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
        if buyer_signed_in?
          format.html { redirect_to ([@user, @order]), notice: 'Order was successfully updated.' }
          format.json { render :show, status: :ok, location: @order }
        else
          format.html { render :edit }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
      end
    end
  end

private

    def order_params
      params.require(:order).permit(:name, :email, :image,:description)
    end

    def order_status
      params.require(:order).permit(:order_status)
    end

    def order_charge
      params.require(:order).permit(:video)
    end
...

我需要完成的工作:

对于每个值,当想要更改order_status时,我希望有一个单独的操作。最重要的是,我希望显示特定的消息,具体取决于order_status列更改为的值。

最重要的问题:

我需要单独的更新操作来向客户收费。这是最重要的,因为当有人更新姓名或卖方更新order_status等时,我不能仅仅向客户收费。

我做错了什么,还是需要设置路由才能执行多个更新操作?

那么我如何分隔更新和参数?

UPDATE

我将更新方法嵌入到一个原始方法中,就像这样...

但是我现在有一个问题,消息通知似乎与更新参数不正确。出现“订单已成功更新”,这不是我上传视频或更改ordeR_status列时的自定义订单

  def update
    respond_to do |format|
      if params["order_status"]
        if user_signed_in?
          format.html { redirect_to ([@user, @order]), notice: 'Order was successfully order_status.' }
          format.json { render :show, status: :ok, location: @order }
        else
          format.html { render :edit }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
      end
        if params["video"]
          @amount = (@order.order_price).to_i * 100
          @amount_seller = (@order.order_price).to_i * 75
          if @order.update(order_charge)
            if user_signed_in? && user.seller?
                          charge = Stripe::Charge.create({
                            :amount      => (@order.order_price).to_i * 100,
                            :description => 'Rails Stripe customer',
                            :currency    => 'usd',
                            :customer => @order.stripe_customer_token,
                            :destination => {
                              :amount => @amount_seller ,
                              :account => (@order.seller.stripe_token),
                            }
                          })
              @order.order_status = "charged"
              format.html { redirect_to ([@user, @order]), notice: 'Order was successfully uploaded.' }
              format.json { render :show, status: :ok, location: @order }
            else
              format.html { render :edit }
              format.json { render json: @order.errors, status: :unprocessable_entity }
            end
          end
        end
        if @order.update(order_params)
          if user_signed_in?
            format.html { redirect_to ([@user, @order]), notice: 'Order was successfully updated.' }
            format.json { render :show, status: :ok, location: @order }
          else
            format.html { render :edit }
            format.json { render json: @order.errors, status: :unprocessable_entity }
          end
          if buyer_signed_in?
            format.html { redirect_to ([@user, @order]), notice: 'Order was successfully updated.' }
            format.json { render :show, status: :ok, location: @order }
          else
            format.html { render :edit }
            format.json { render json: @order.errors, status: :unprocessable_entity }
          end
        end
      end
    end

所以我现在有两个问题:

  1. 自定义消息不显示
  2. 并且在:video参数更新下,我希望订单状态更改“已收费”,但是我目前没有这样做

有人对如何改善它有任何想法吗?

1 个答案:

答案 0 :(得分:0)

到目前为止,这似乎已经解决了。 :::

def update
    respond_to do |format|
      if @order.update(order_status)
        if user_signed_in?
          format.html { redirect_to ([@user, @order]), notice: 'Order was successfully order_status.' }
          format.json { render :show, status: :ok, location: @order }
        else
          format.html { render :edit }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
      end
        if @order.update(order_charge)
          @amount = (@order.order_price).to_i * 100
          @amount_seller = (@order.order_price).to_i * 75
          if @order.update(order_charge)
            if current_user.seller?
                          charge = Stripe::Charge.create({
                            :amount      => (@order.order_price).to_i * 100,
                            :description => 'Rails Stripe customer',
                            :currency    => 'usd',
                            :customer => @order.stripe_customer_token,
                            :destination => {
                              :amount => @amount_seller ,
                              :account => (@order.seller.stripe_token),
                            }
                          })
              @order.order_status = "charged"
              format.html { redirect_to ([@user, @order]), notice: 'Order was successfully uploaded.' }
              format.json { render :show, status: :ok, location: @order }
            else
              format.html { render :edit }
              format.json { render json: @order.errors, status: :unprocessable_entity }
            end
          end
        end
        if @order.update(order_params)
          if user_signed_in?
            format.html { redirect_to ([@user, @order]), notice: 'Order was successfully updated.' }
            format.json { render :show, status: :ok, location: @order }
          else
            format.html { render :edit }
            format.json { render json: @order.errors, status: :unprocessable_entity }
          end
          if buyer_signed_in?
            format.html { redirect_to ([@user, @order]), notice: 'Order was successfully updated.' }
            format.json { render :show, status: :ok, location: @order }
          else
            format.html { render :edit }
            format.json { render json: @order.errors, status: :unprocessable_entity }
          end
        end
      end
    end

private

    def order_params
      params.require(:order).permit(:name, :email, :image, :video, :description)
    end

    def order_status
      params.require(:order).permit(:order_status)
    end

    def order_charge
      params.require(:order).permit(:video)
    end
end

如果有人看到这并认为他们有更好的方法,请告诉我。我不确定这可能有多方便或有效率(将所有内容嵌入一种方法等),但它似乎可行。我唯一的问题是

  1. 当我将order_status更改为取消时,没有消息出现,但可以使用。

  2. 当我更改视频参数填充时,会显示order_status消息。我理解但我不理解的原因是,当我将状态更改为取消时,为什么它不会出现-顺便说一句,取消是枚举“ 2”。