gpt4 book ai didi

ruby-on-rails - 如何在每次单击按钮时更新表中的 'status' 值

转载 作者:行者123 更新时间:2023-12-02 21:06:55 28 4
gpt4 key购买 nike

当我单击每个按钮时,我想将 properties 表中的 status 值更新为(1 或 2 或 3 或 4)。

这些是我的 View 文件中的按钮:

<td><%= link_to("Waiting for Response", rms_property_approve_property_path(property, {:status => 'Waiting for Response'}), method: :patch, class: "btn btn-success", "data-no-turbolink" => true) %><td>
<td><%= link_to("No Response", rms_property_approve_property_path(property, {:status => 'No Response'}), method: :patch, class: "btn btn-danger", "data-no-turbolink" => true) %><td>
<td><%= link_to("Registered", rms_property_approve_property_path(property, {:status => 'Registered'}), method: :patch, class: "btn btn-success", "data-no-turbolink" => true) %><td>
<td><%= link_to("Not Interested", rms_property_approve_property_path(property, {:status => 'Not Interested'}), method: :patch, class: "btn btn-danger", "data-no-turbolink" => true) %><td>

我的properties_controller.rb:

  def approve
@property = Property.find(params[:property_id])
if params[:status]== 'Registered'
@property.update_attributes(:status => 1)
redirect_to :back, flash: {notice: "Property has been Registered."}
elsif params[:status]== 'Not Interested'
@property.update_attributes(:status => 2)
redirect_to :back, flash: {notice: "Not Interested."}
elsif params[:status]== 'Waiting for Response'
@property.update_attributes(:status => 3)
redirect_to :back, flash: {notice: "Waiting for Response"}
elsif params[:status]== 'No Response'
@property.update_attributes(:status => 4)
redirect_to :back, flash: {notice: "No Response."}
end
end

我的 properties 表中状态列的迁移文件:

class AddColumnStatusInProperties < ActiveRecord::Migration
def change
add_column :properties, :status, :string
end
end

当我单击无响应按钮时,我收到一个 ArgumentError:

'4' is not a valid status

enter image description here

最佳答案

从错误消息来看,您似乎正在使用 enum状态列上。除非您跳过对象实例化(例如,使用 update_allupdate_columns),否则不能将原始值(枚举值的整数部分)与枚举一起使用。

如果实例化对象,则必须使用枚举值(值为 :registered,而原始值为 1)。

approve中,您需要更新对象,如下所示:

# `:registered` should be the enum value, not the number
@property.update_attributes(status: :registered)

不是

@property.update_attributes(status: 4)

这假设您已声明您的枚举:

class Property < ActiveRecord::Base
enum status: {
registered: 1,
not_interested: 2,
waiting_for_response: 3, # consider renaming to `awaiting_response`
registered: 4
}
end

您应该将迁移中的列类型更改为整数。使用字符串可能会导致奇怪的错误。

rails g migration change_status_column_type_in_properties

class ChangeStatusColumnTypeInProperties < ActiveRecord::Migration
def change
change_column :properties, :status, :integer
end
end

您还可以在 View 中自动生成链接:

<% Property.statuses.each_key do |name| %>
<%= link_to name, rms_property_approve_property_path(property, {status: name}), method: :patch, class: "btn btn-danger", "data-no-turbolink" => true) %>
<% end %>

并简化 Controller 代码:

def approve
@property = Property.find(params[:property_id])
@property.update!(status: params[:status])
redirect_to :back, notice: t(".#{params[:status]}")
end

并将闪现消息添加到您的区域设置文件中。例如:

en:
rms:
properties:
approve:
registered: "Property registered"
waiting_for_response: "..."

最后,考虑为您的列使用默认值。

change_column :properties, :status, :integer, null: false, default: 3

关于ruby-on-rails - 如何在每次单击按钮时更新表中的 'status' 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35595408/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com