嘿,我正在用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 DashboardController < ApplicationController
before_filter :authorize, only: [:index]
def index
@posts = Post.order("created_at DESC")
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
註釋型號
class Comment < ActiveRecord::Base
belongs_to :post
end
條
終於路線
resources :dashboard
resources :posts do
resources :comments
end
再次,我非常新的後臺開發和紅寶石,所以如果需要的話我可以提供更多的信息。我想如果我可以把所有的意見都放到儀表板中,我就可以找出其他問題。
謝謝!
只是一個方面,但不是我要改變'@ post.update(@ params [:post] .permit(etc))'到'@ post.update(post_params)'。保持您的代碼乾爽。 – CWitty