Ruby on rails 将状态从挂起更改为已批准

Ruby on rails 将状态从挂起更改为已批准,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,你好这里我有一个haml索引 %h1 Requests %table %thead %tr %th Request Template %th User %th Sent At %th Status %tbody - @requests.each do |request| %tr %td= link_to request.request_template_id, admin_request_template(requests.reques

你好这里我有一个haml索引

%h1 Requests
%table
%thead
  %tr
    %th Request Template
    %th User
    %th Sent At
    %th Status
%tbody
  - @requests.each do |request|
    %tr
      %td= link_to request.request_template_id, admin_request_template(requests.request_template_id)
      %td= request.user_id
      %td= request.request_sent_at
      %td= request.status
如果您看到最后一行显示request.status。现在,它使用请求模型中的after_保存显示默认设置为pending的状态

我希望用户单击状态并将其更改为“已批准”。如何执行此操作?我有一个可以显示状态的数组。我还有一个Requests表的status列

请参阅下面我的请求模型

class Request < ActiveRecord::Base
  belongs_to :request_template
  belongs_to :user

  has_many   :request_answers, autosave: true

  validates :request_template, :user, presence: true

  after_create :send_notification, :status_pending

  STATUSES= %w[pending approved]

  def send_notification
    RequestMailer.delay.new_request_admin_notification(id)
    request_notification_sent_date!
  end

  def request_notification_sent_date!
    update_attribute :request_sent_at, Time.current
  end

  def pending!
    update_attribute :status, "pending"
  end

  def approved!
    update_attribute :status, "approved"
  end
end
类请求
在您的
请求旁边。状态
您可以有一个指向更新操作的链接:

链接到'Approve',请求路径(请求,请求:{status:'approved'}),方法:put

如果您使用的是强参数,则应将您的
请求控制器设置为接受
状态
参数

PS:与问题无关,但如果您将
状态
列的默认值设置为
挂起
,则您不需要在创建
后使用
回调来设置状态