将状态从待定更改为已批准
Change Status from Pending to Approved
你好,我有一个 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_save 显示默认设置为挂起的状态。
我会喜欢用户点击状态并将其更改为已批准。我该怎么做? 我有一个可以显示状态的数组。我还有一个请求 table 的状态列。
请在下面查看我的请求模型。
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
在您的 request.status
旁边,您可以 link 更新操作:
link_to 'Approve', request_path(request, request: {status: 'approved'}), method: put
.
如果您使用强参数
,您的 RequestsController
应该设置为接受 status
参数
PS:与问题无关,但是如果您将 status
列的默认值设置为 pending
,那么您就不需要 after_create
回调设置状态。
你好,我有一个 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_save 显示默认设置为挂起的状态。
我会喜欢用户点击状态并将其更改为已批准。我该怎么做? 我有一个可以显示状态的数组。我还有一个请求 table 的状态列。
请在下面查看我的请求模型。
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
在您的 request.status
旁边,您可以 link 更新操作:
link_to 'Approve', request_path(request, request: {status: 'approved'}), method: put
.
如果您使用强参数
,您的RequestsController
应该设置为接受 status
参数
PS:与问题无关,但是如果您将 status
列的默认值设置为 pending
,那么您就不需要 after_create
回调设置状态。