【问题标题】:Rails update action fails to check paramsRails 更新操作无法检查参数
【发布时间】:2021-09-10 05:21:47
【问题描述】:

我正在构建一个 Rails API,发现 put 请求在没有必需参数的情况下通过。这对我来说很奇怪,因为应用程序不允许没有参数的发布请求。此外,当我尝试通过 Rails 控制台更新没有属性的支出时,它会失败。但是通过 Postman/CURL 请求成功通过

控制器如下所示:

class SpendingsController < ApplicationController
  before_action :find_spending, only: %i[show update destroy]

  def create
    spending = Spending.new(spending_params)
    spending.user = current_user
    spending.category = Category.find_by(id: spending_params[:category_id])

    if spending.valid?
      spending.save
      render json: SpendingSerializer.new(spending), status: :ok
    else
      render json: ActiveRecordErrorsSerializer.new(spending), status: :bad_request
    end
  end

  def index
    spendings = Spending.where(user_id: current_user.id).order("#{sort_spendings}")
    total_value = Spending.where(user_id: current_user.id).pluck(:amount).sum

    render json: {spendings: SpendingSerializer.new(spendings), total_amount: total_value}, status: :ok
  end

  def show
    if @spending.valid?
      render json: SpendingSerializer.new(@spending), status: :ok
    else
      render json: ActiveRecordErrorsSerializer.new(@spending), status: :not_found

    end
  end

  def update
    if @spending.valid?
      @spending.update(spending_params)
      render json: SpendingSerializer.new(@spending), status: :ok
    else
      render json: ActiveRecordErrorsSerializer.new(@spending), status: :bad_request
    end
  end

  def destroy
    if @spending.destroy
      head :no_content
    else
      render json: ActiveRecordErrorsSerializer.new(@spending), status: :not_found
    end
  end

  private

  def spending_params
    params.require(:spending).permit(:description, :amount, :category_id)
  end

  def find_spending
    begin
      @spending = Spending.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      render json: {errors: "Spending with id #{params[:id]} not found"}, status: :not_found
    end
  end

  def sort_spendings
    sort = { sort_by: "created_at", sort_dir: "desc"}
    sort[:sort_by] = params[:sort_by].split(" ").first if params[:sort_by].present?
    sort[:sort_dir] = params[:sort_by].split(" ").last if params[:sort_by].present?
    sort.values.join(" ")
  end
end

还有我的模特:

class Spending < ApplicationRecord
  belongs_to :user
  belongs_to :category
  validates :description,
            presence: true
end

我真的没有想法,为什么会这样。猜猜这与什么有关?

【问题讨论】:

  • PUT 请求不需要具有所有必需的参数,因为它只更新已经存在的记录并且这些记录已经设置了所有必需的属性。
  • 非常感谢!我刚刚意识到我试图捕捉的不是错误而是标准行为,猜想,我对第一次编码工作的测试任务压力太大了。当我说非常感谢时,我是认真的!

标签: ruby-on-rails ruby put


【解决方案1】:

我注意到的第一件事是您的update 方法。在更新模型之前检查验证。 @spending.valid? 在这种情况下总是返回 true。我的建议是修改它。 @spending.update(spending_params) 更新成功返回true,失败返回false。

def update
  if @spending.update(spending_params)
    render json: SpendingSerializer.new(@spending), status: :ok
  else
    render json: ActiveRecordErrorsSerializer.new(@spending), status: :bad_request
  end
end

created 方法也可以优化。您不需要单独查找和分配category。它将被分配为所有spending_params

def create
  spending = Spending.new(spending_params)
  spending.user = current_user

  spending.save
    render json: SpendingSerializer.new(spending), status: :ok
  else
    render json: ActiveRecordErrorsSerializer.new(spending), status: :bad_request
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多