如何在每次单击按钮时更新表中的“状态”值(How to update 'status' value in table on each button click)

14 IT屋

I want to update the status value in the properties table as (1 or 2 or 3 or 4) when I click on each button.

These are my buttons in my view file:

<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>

My 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

My migration file for status column in properties table:

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

When I click on the No response button, I get an ArgumentError:

'4' is not a valid status

enter image description here

解决方案

Judging by the error message, it seems that you are using an 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, for example).

If you instantiate the object, you must use the enum value (value is :registered, while raw value is 1).

In approve, you need to update the object as such:

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

And not

@property.update_attributes(status: 4)

This assumes you have declared your enum as such:

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

You should change the column type in the migration to integer. Using string can lead to strange bugs.

rails g migration change_status_column_type_in_properties

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

You can also automate the link generation in your 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 %>

And simplify the controller code:

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

And add the flash messages to your locale file. For example:

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

Finally, consider using a default value to your column.

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

我想将属性表中的状态值更新为(1或2或3或4)当我单击每个按钮时。



这些是我在视图文件中的按钮:



 < code>< td><%= link_to(" Waiting for Response",rms_property_approve_property_path(property,{:status =>'Waiting for Response'}),方法::patch,类:" btn btn-成功"," data-no-turbolink" => true)%< td> 
< td><%= link_to("无响应",rms_property_approve_property_path(property,{:status =>'无响应'}),方法::patch,类:" btn btn-danger", " data-no-turbolink" => true)%< td>
< td><%= link_to(" Registered",rms_property_approve_property_path(property,{:status =>'Registered'}),方法::patch,类:" btn btn-success","数据-no-turbolink" => true)%< td>
< td><%= link_to("不感兴趣",rms_property_approve_property_path(property,{:status =>'不感兴趣'}),方法::patch,类:" btn btn-danger", " data-no-turbolink" => true)%< td>


我的 properties_controller.rb :



  def批准
@property = Property.find(params [:property_id])
if params [:status] =='已注册'
@ property.update_attributes(:status => 1)
redirect_to:back,flash:{注意:"属性已注册。"}
elsif params [:状态] =='不感兴趣'
@ property.update_attributes(:status => 2)
redirect_to:back,flash:{注意:"不感兴趣。"}
elsif params [ :status] =='等待响应'
@ property.update_attributes(:status => 3)
redirect_to:back,flash:{注意:"等待响应"}
elsif params [:status] =='无响应'
@ property.update_attributes(:status => 4)
redirect_to:back,flash:{注意:"无响应"。}}
结束
结束


properties < / code> t能够:



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


当我单击无响应按钮时,出现ArgumentError:



 '4'不是有效状态



解决方案

从错误消息来看,您似乎正在使用 枚举 在< code>状态列。除非您跳过对象实例化(使用原始值(枚举值的整数部分)。 / ActiveRecord / Relation / update_all" rel =" nofollow"> update_all update_columns ,例如。)



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

在批准中,您需要这样更新对象:



 #`:registered`应该是枚举值,而不是数字
@ property.update_attributes(status::registered)


而不是



  @ property.update_attributes (状态:4)


这假设您已经声明了枚举:



  class属性< ActiveRecord :: Base 
枚举状态:{
已注册:1,
not_interested:2,
wait_for_response:3,#考虑重命名为`awaiting_response`
已注册:4
}
结尾


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



  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做|名称| %> 
<%= link_to名称,rms_property_approve_property_path(属性,{状态:名称}),方法::patch,类:" btn btn-danger"," data-no-turbolink" => true)%>
<%end%>


并简化控制器代码:



  def批准
@property = Property.find(params [:property_id])
@ property.update!(status:params [:status])
redirect_to:back,注意:t("。#{params [:status]}")
结束


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



  en:
rms:
属性:
批准:
注册:"财产注册"
等待响应:" ..."


最后,考虑



  change_column:properties,:status,:integer,null:false,默认值:3 

本文地址:IT屋 » 如何在每次单击按钮时更新表中的“状态”值