【问题标题】:How to update 'status' value in table on each button click如何在每次单击按钮时更新表中的“状态”值
【发布时间】:2016-02-24 07:04:09
【问题描述】:

单击每个按钮时,我想将properties 表中的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 表中状态列的迁移文件:

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

当我点击 No response 按钮时,我得到一个 ArgumentError:

'4' is not a valid status

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 activerecord


    【解决方案1】:

    从错误消息来看,您似乎在status 列上使用了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
    

    您应该将迁移中的列类型更改为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
    

    【讨论】:

    • 它工作正常。感谢您纠正我的错误,也感谢您提供如此详细的答案。非常感谢@Mohamad
    猜你喜欢
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2020-01-12
    • 1970-01-01
    • 2019-07-19
    相关资源
    最近更新 更多