使用<%=渲染评论%>即使数据库中没有任何内容,也始终输出空的部分

时间:2013-06-19 12:52:04

标签: ruby-on-rails ruby-on-rails-4

我开始怀疑这是否是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控制台,看看特定帖子有多少评论,只是为了确保数据库中没有一些空记录,但事实并非如此。为什么我得到这个空的和额外的部分与数据库记录不匹配?

2 个答案:

答案 0 :(得分:1)

这是因为@post.comments.build中的comments/form.html.erbbuild()将始终创建一个新的空对象,它位于render()之前。请尝试将其替换为Comment.new,以便空评论不会与帖子相关联。

答案 1 :(得分:1)

问题是表单位于<%= render @post.comments %>之上,因此当您到达所有注释的部分内容时会有空注释,即使数据库中没有注释,也请将下面的<%= render "comments/form" %>置于修复状态此