ruby - Getting Comments into Dashboard Rails -
hey creating blog rails, first app. trying make comments have approved first via dashboard. code currently.
the posts controller
class postscontroller < applicationcontroller before_filter :authorize, only: [:edit, :update, :destroy, :create, :new] def index @posts = post.where(:state => "published").order("created_at desc") end def new @post = post.new end def show @post = post.find(params[:id]) redirect_to_good_slug(@post) , return if bad_slug?(@post) end def create @post = post.new(post_params) if @post.save redirect_to dashboard_path else render 'new' end end def edit @post = post.find(params[:id]) end def update @post = post.find(params[:id]) if @post.update(params[:post].permit(:title, :text, :author, :short, :photo, :state)) redirect_to dashboard_path else render 'edit' end end def destroy @post = post.find(params[:id]) @post.destroy redirect_to dashboard_path end private def post_params params.require(:post).permit(:title, :text, :author, :short, :photo, :state) end end the dashboard controller
class dashboardcontroller < applicationcontroller before_filter :authorize, only: [:index] def index @posts = post.order("created_at desc") end end the comments controller
class commentscontroller < applicationcontroller before_filter :authorize, only: [:destroy] def create @post = post.find(params[:post_id]) @comment = @post.comments.create(comments_params) redirect_to post_path(@post) end def destroy @post = post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @comment.destroy redirect_to post_path(@post) end private def comments_params params.require(:comment).permit(:commenter, :body) end end the comment model
class comment < activerecord::base belongs_to :post end and routes
resources :dashboard resources :posts resources :comments end again, pretty new backend dev , ruby, if need can provide more info. think if can pull comments dashboard able figure rest out.
thanks!
you want create dashboard controller users access approve can access. index action can like.
def index @posts = post.find(approved: false) end you can add scope post model
class post < activerecord::base scope :approved, -> {where(approved: false)} end you can
def index @posts = post.approved end
Comments
Post a Comment