Ruby on rails 将评论导入仪表板轨道

Ruby on rails 将评论导入仪表板轨道,ruby-on-rails,ruby,Ruby On Rails,Ruby,嘿,我正在用我的第一个应用程序rails创建一个博客。我试图通过仪表板发表必须首先批准的评论。这是我目前的代码 邮政署署长 class PostsController < ApplicationController before_filter :authorize, only: [:edit, :update, :destroy, :create, :new] def index @posts = Post.where(:state => "publ

嘿,我正在用我的第一个应用程序rails创建一个博客。我试图通过仪表板发表必须首先批准的评论。这是我目前的代码

邮政署署长

 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) and 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
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
同样,我对后端开发和ruby还很陌生,所以如果需要,我可以提供更多信息。我想,如果我能把所有的评论都放到仪表板上,我就可以把其余的都弄清楚了


谢谢

您需要创建一个
仪表板
控制器,具有审批权限的用户可以访问该控制器。对于
索引
操作,您可以执行以下操作

def index
  @posts = Post.find(approved: false)
end
您还可以向
Post
模型添加范围

class Post < ActiveRecord::Base
  scope :approved, -> {where(approved: false)}
end

只是一个侧面,但我会将
@post.update(@params[:post].permit(etc))
更改为
@post.update(post_-params)
。让你的代码保持干爽。我的帖子会显示在仪表板上,但就我个人而言,我无法将评论拉到视图中。我目前可以在仪表板中完成所有的后期crud。编辑/删除文章,在编辑视图中,我已经将其设置为处理草稿/发布状态。只是没有运气把评论拉到实际的仪表板视图中。这就是它所指的吗?我在原来的帖子中添加了当前的仪表板控制器。我认为我需要能够做的是将所有未批准的评论转储到仪表板视图中,然后从那里我可以在它们出现时删除或批准它们。你应该能够直接从post对象中将其引用为post.comments。当我尝试将post.comments添加到视图/仪表板/索引中时,我发现这个错误,你也可以这样做@comments=Comment.where(id:@posts.map{| post | post.id})
 resources :dashboard
 resources :posts do
  resources :comments
 end
def index
  @posts = Post.find(approved: false)
end
class Post < ActiveRecord::Base
  scope :approved, -> {where(approved: false)}
end
def index
  @posts = Post.approved
end