0

嗨,伙计们,显然每个新来的人都知道并且对糟糕的文档感到愤怒

acts_as_commentable_with_threading

提供 - 我也不例外。

我有一个 Post 模型,我已经成功了

acts_as_commentable

按照文档中的要求。此外,我在帖子的 show 方法中添加了所需的代码。哪个是:

def show
 @post = Post.find(params[:id])
 @comments = @post.comment_threads.order('created_at desc')
 @new_comment = Comment.build_from(@post, current_user, "")
end

在节目中我有:

<%= form_for @new_comment do |f| %>

  <%= f.label :body, 'Comment' %>
  <%= f.text_area :body %>

  <%= f.submit 'Post comment' %>

<% end %>

现在显然我意识到我需要在comments_controller 中有一个创建操作。但是我不知道要写什么才能成功保存评论。有什么帮助吗?

4

1 回答 1

0

你应该这样写:

def create
    # Check structure of incomming parameters (use strong_params gem integrated with Rails)
    comment_params = params.requires[:comment].permit(:body)

    # Assign filtered parameters to new comment
    @comment = @post.comment_threads.build(comment_params)

    # Save it
    if @comment.save
        # If success then redirect to any page i.e. to your page with posts
        redirect_to { action: :index }, { notice: 'Your comment was saved!' }
    end

    # If not success then render form with error again
    render action: :show
end
于 2015-03-02T13:30:56.487 回答