如何在每次单击按钮时更新 table 中的 'status' 值
How to update 'status' value in table on each button click
我想在单击每个按钮时将 properties
table 中的 status
值更新为(1 或 2 或 3 或 4)。
这些是我在视图文件中的按钮:
<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
table:
class AddColumnStatusInProperties < ActiveRecord::Migration
def change
add_column :properties, :status, :string
end
end
当我点击 No response
按钮时,出现 ArgumentError:
'4' is not a valid status
从错误消息来看,您似乎使用了 enum
on the status
column. You can't use raw values (the integer part of the enum value) with enums unless you skip object instantiation (using update_all
, or update_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
您应该将迁移中的列类型更改为 integer
。使用字符串会导致奇怪的错误。
rails g migration change_status_column_type_in_properties
class ChangeStatusColumnTypeInProperties < ActiveRecord::Migration
def change
change_column :properties, :status, :integer
end
end
您还可以在您的视图中自动生成 link:
<% 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 %>
并简化控制器代码:
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
我想在单击每个按钮时将 properties
table 中的 status
值更新为(1 或 2 或 3 或 4)。
这些是我在视图文件中的按钮:
<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
table:
class AddColumnStatusInProperties < ActiveRecord::Migration
def change
add_column :properties, :status, :string
end
end
当我点击 No response
按钮时,出现 ArgumentError:
'4' is not a valid status
从错误消息来看,您似乎使用了 enum
on the status
column. You can't use raw values (the integer part of the enum value) with enums unless you skip object instantiation (using update_all
, or update_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
您应该将迁移中的列类型更改为 integer
。使用字符串会导致奇怪的错误。
rails g migration change_status_column_type_in_properties
class ChangeStatusColumnTypeInProperties < ActiveRecord::Migration
def change
change_column :properties, :status, :integer
end
end
您还可以在您的视图中自动生成 link:
<% 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 %>
并简化控制器代码:
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