当我点击每个按钮时,我想将status
表中的properties
值更新为(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
表中的状态列的迁移文件:
class AddColumnStatusInProperties < ActiveRecord::Migration
def change
add_column :properties, :status, :string
end
end
当我点击No response
按钮时,我收到一个ArgumentError:
'4' is not a valid status
答案 0 :(得分:4)
根据错误消息判断,您似乎在status
列上使用了enum
。除非您跳过对象实例化(例如,使用update_all
或update_columns
),否则不能将原始值(枚举值的整数部分)与枚举一起使用。< / p>
如果您实例化对象,则必须使用枚举值(值为: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
您还可以在视图中自动生成链接:
<% 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
将flash消息添加到您的语言环境文件中。例如:
en:
rms:
properties:
approve:
registered: "Property registered"
waiting_for_response: "..."
最后,请考虑使用列的默认值。
change_column :properties, :status, :integer, null: false, default: 3