Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 是否自定义更新时每个参数的通知消息?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 是否自定义更新时每个参数的通知消息?

Ruby on rails 是否自定义更新时每个参数的通知消息?,ruby-on-rails,ruby,Ruby On Rails,Ruby,目标:为每个参数或参数集提供唯一的消息 我的控制器更新中有此代码: def update @order = Order.find(params[:id]) respond_to do |format| if @order.update_attributes(order_status) # if @order.changed == [:order_status] format.html { redirect_to @order, no

目标:为每个参数或参数集提供唯一的消息

我的控制器更新中有此代码:

def update
    @order = Order.find(params[:id])
    respond_to do |format|
      if @order.update_attributes(order_status)
        # if @order.changed == [:order_status]
          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
    elsif @order.update(order_params)
        format.html { redirect_to @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

private

def order_params
      params.require(:order).permit(:name, :name_2, :email, :phone_number)
    end

    def order_status
      params.require(:order).permit(:order_status)
    end
尽管如此,无论我更新哪个参数,我总是会收到消息“订单已成功取消”,即使我根本不更新
:Order\u status

我尝试了许多组合,但我无法得到一个独特的信息来工作。一切仍在更新,只是没有我想要的消息。我尝试过不使用
elsif
、不定义
@order
、不同的订单、用户
.update
.update\u属性
、使用
@order.update(参数[:order\u status])


我可以在这里做什么来获得自定义消息?

假设您没有为
参数[:order\u status]
提供值。然后方法
order\u status
将返回一个空哈希。从技术上讲,它是的一个实例,但其行为类似于散列:

ActionController::Parameters.superclass
# => ActiveSupport::HashWithIndifferentAccess

ActionController::Parameters.superclass.superclass
# => Hash

params = ActionController::Parameters.new(order: {foo: "bar"})
params.require(:order).permit(:order_status)
# => {}
运行或使用空哈希将成功。这就是满足第一个
条件的原因。作为修复,您可以做一些非常简单的事情:

if order_stats.present? && @order.update_attributes(order_status)
该方法将检查对象是否真实且非空。这与


Rails用于在更新期间和更新后跟踪对模型的更改。您还可以使用它查看更改了哪些属性(
@order.order\u status\u changed?
),它们最初是什么,以及它们也更改了什么(
@order.order\u status\u changed?(from::open,to::cancelled)
)。这将使您能够更好地控制响应。请注意,在保存order对象之前,需要实现最后两个示例。

我认为这些更改仅在模型的回调周期中可用。比如更新之后的
,\u保存之后的
,等等。但是OP可以进行这样的回调,并使用虚拟属性将它们传递给控制器。如果你查看我链接的文档,它在回调之外可用。哦,我的错。我刚刚用我的工作应用程序进行了测试,他们一定没有以支持的方式使用
ActiveRecord::Dirty
配置模型。快速测试有效,谢谢<代码>如果@order.update\u属性(订单状态)和&@order.order\u状态之前发生了更改?
有效,并且两条消息都有效。将精简代码。非常感谢。
if @order.update(order_status)
  if @order.order_status_previously_changed?
    format.html { redirect_to @order, notice: 'Order was successfully cancelled.' }
    ...