我没有使用 Audited gem 来提供“草稿”模式。相反,我在模型中添加了一个名为active的布尔值,并声明
default_scope { where(active: true) }
scope :active, -> { where(active: true ) }
scope :draft, -> { where(active: false) }
然后,在控制器中,管理员查看草稿项的方法:
def in_draft
# Admins can see all items in draft status.
# Sellers can see only their own items in draft status.
# Buyers can't get here at all because of the authorizations
if current_tuser.role == "Themadmin"
@seller_listings = Listing.unscoped.draft
end
end
最后,在控制器中发布项目的方法:
80 def publish
81 @listing = Listing.unscoped.find(params[:id])
82 @listing.active = true
83 respond_to do |format|
84 if @listing.save!
85 format.html {redirect_to @listing, notice: 'Listing changed from draft to published.'}
86 else
87 format.html {redirect_to @listing, notice: 'Something went wrong'}
88 end
89 end
90 end