作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我单击每个按钮时,我想将 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
最佳答案
从错误消息来看,您似乎正在使用 enum
在状态
列上。除非您跳过对象实例化(例如,使用 update_all
或 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
您应该将迁移中的列类型更改为整数
。使用字符串可能会导致奇怪的错误。
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/
我是一名优秀的程序员,十分优秀!