1

我开始怀疑这是否是 Rails 4 的一个错误,但我对 Rails 很陌生,最终发现自己遇到了大多数错误。但我在这个问题上碰壁了。

我有一个帖子。帖子有评论。

我的评论部分 (/views/comments/_comment.html.erb)

<div class="comment-wrap">
<div class="row">
<div class="comment-meta">
    <%= comment.commenter %>
    <small><%= comment.created_at %></small>
    <%= link_to 'Destroy', [comment.post, comment], method: :delete,  confirm: 'Are you sure?', class: "tiny button radius right" %>
</div>
<div class="small-2 columns">
  <img src="http://placehold.it/50" height="50" width="50" alt="Avatar Image"/>
</div>

<div class="small-10 columns">
  <%= comment.body %>
</div>

</div>
</div>

这是我在 .../views/posts/show.html.erb 中呈现的方式

<h4>Leave a comment</h4>
<%= render "comments/form" %>

<h4>Comments</h4>
<%= render @post.comments %>

编辑:控制器 .../controllers/comments/comments_controller.rb

class CommentsController < ApplicationController
    def create
        @post = post.find(params[:post_id])
        @comment = @post.comments.create(comment_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 comment_params
      params.require(:comment).permit(:commenter, :body, :post_id)
    end
end

编辑:帖子控制器

class postsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]

# GET /posts
# GET /posts.json
def index
  @posts = post.all
end

# GET /posts/1
# GET /posts/1.json
def show
end

# GET /posts/new
def new
  @post = post.new
end

# GET /posts/1/edit
def edit
end

# POST /posts
# POST /posts.json
def create
  @post = post.new(post_params)

  respond_to do |format|
    if @post.save
      format.html { redirect_to @post, notice: 'post was successfully created.' }
      format.json { render action: 'show', status: :created, location: @post }
    else
      format.html { render action: 'new' }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
  respond_to do |format|
    if @post.update(post_params)
      format.html { redirect_to @post, notice: 'post was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
  @post.destroy
  respond_to do |format|
    format.html { redirect_to posts_url }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_post
    @post = post.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def post_params
    params.require(:post).permit(:name, :description, :date, :address)
  end
end

这将列出评论,但第一项始终是空的部分,带有占位符头像和删除按钮。我已经在 Rails 控制台中检查了特定帖子有多少评论,只是为了确保数据库中没有空记录,但事实并非如此。为什么我得到这个与数据库记录不匹配的空的和额外的部分?

4

2 回答 2

1

问题是表单在上面,<%= render @post.comments %>所以当你到达所有评论的部分时,会有一个空评论,即使数据库中没有,也可以在<%= render "comments/form" %>下面解决这个问题。

于 2015-04-02T04:41:07.900 回答
1

这是因为@post.comments.buildcomments/form.html.erb. build()将始终创建一个新的空对象,并且在render(). 尝试将其替换为,Comment.new以便空评论不会与帖子相关联。

于 2013-10-07T06:35:41.173 回答