2011-05-16 56 views
0

我已创建了迁移t.boolean :published, :default =>false如何使博客只能由管理员发布?

一个博客的模型,现在我不知道如何使这个博客帖子是在默认情况下,只有管理员可以发布取消发布。授权部分可以用cancan完成,模型和控制器如何?

这是我当前的代码: 型号/ blog.rb

class Blog < ActiveRecord::Base 
    attr_accessible :title, :content, :user_id, :published 
    has_many :comments, :as => :commentable 

    def published? 
    published 
    end 
    def published! 
    self.published = true 
    end 
    def unpublished! 
    self.published = false 
    end 
end 

控制器我使用make_resourceful插件来处理嵌套和多态

class BlogsController < ApplicationController 
    make_resourceful do 
    actions :all 
response_for :create do 
    flash[:notice] = "Successfully created article." 
     redirect_to blogs_url 
    end 
response_for :update do 
     flash[:notice] = "Successfully updated article." 
     redirect_to blogs_url 
    end 
response_for :destroy do 
     flash[:notice] = "Successfully destroyed article." 
     redirect_to blogs_url 
    end 
end 
end 

任何想法家伙/ blog_controller.rb# ??或者一个有用的链接?谢谢!

回答

0

我不确定我是否遵循您的要求,如果您使用的是CanCan,那么您的authorizations.rb文件应该包含管理员用户可以执行的操作以及常规用户可以执行的操作。

https://github.com/ryanb/cancan/wiki/Defining-Abilities

取决于你所使用的身份验证,但如果是像设计与内置的助手,你也可以使用before_filter :authenticate_admin!, :only => [:edit, :update]测试,确保当前用户是管理员。 (我建议使用白名单:除了:仅限于:)。

+0

thnks为答复..然后如何显示只有:发布与真正被显示??我应该在def index上写什么? – Iqbal 2011-05-16 18:25:36

+0

我想,我发现了适合我需要的插件.. https://github.com/mysmallidea/requires-approval – Iqbal 2011-05-16 18:48:37

相关问题