找不到'id'=的评论

时间:2015-10-03 18:38:10

标签: ruby-on-rails comments

我正在尝试确保评论可以属于帖子,并且具有属于每个用户的ID。意思是,我希望能够只是呈现评论并让它说明哪个用户发布了该评论。到目前为止,通过同样的事情阅读其他人的问题,我仍然无法解决它!我感谢任何帮助,并提前感谢!评论不会在仪表板上呈现。

这是我的users_controller.rb(仅限有用位),

class UsersController < ApplicationController
  before_action :set_user, only: [:edit, :update, :destroy]
  before_action :correct_user,   only: [:edit ]

  after_action :signed_in_after_register, only: :create 

  def dashboard 
    @user = User.find(session[:user_id]) unless session[:user_id] == ""
    redirect_to login_path, notice: "You're not logged in" unless @user 
    @posts = @user.posts.order("created_at DESC").limit(3)
    @comment = Comment.find(params[:comment_id])
  end 

    def user_params
      params.require(:user).permit(:first_name, :last_name, :bio, :comments, :password, :password_confirmation, :email, :age, :profile_picture, :post, :body)
    end
end

这是我的dashboard.html.erb,

<%- @posts.each do |post| %>
    <div class="panel panel-default">
        <div class="panel-heading-gray"> <%= image_tag @user.profile_picture.url(:thumb) %> <h5 class="user-name"><%= @user.name %></h5> <h6 class="time-posted"><%= post.created_at.strftime("%B, %d, %Y") %></h6></div>
        <div class="panel-body"><%= link_to post.body, post %></div>
        <div class="panel-footer"> 
            <h2>Add a comment:</h2>
            <%= render post.comments %>
            <p class="Like-option">Like ·</p>
            <p class="comment-form">Comment - <%= @post.comments.count %></p>  
            <p class="view-option">· View</p>
            <p class="comment-profile-picture">
            <%= image_tag @user.profile_picture.url(:thumb) %></p>
            <div class="comments-stream">
            <%= render @post.comments %>
        </div>
        <div id="comments-form">
            <%=  render "comments/form", :post => post %>
        </div>  
    </div> 
<!-- Other non-comments related code here -->
<% end %>

编辑:这是我的_comment.html.erb,

<div class="comment clearfix">
    <div class="comment_content">
        <p class="comment_body"><%= comment.body %></p>

        <% if @user_signed_in %>
            <p><%= link_to "Delete", [comment.post, comment],
                        method: :delete, 
                        class: "button",
                        data: { confirm: "Are you sure?" } %>
            </p>
        <% end %>
    </div>
</div>

以防万一这是我的评论表单_form.html.erb,

<%= form_for([@post, @post.comments.build]) do |f| %>
    <p>
        <%= f.text_area :body, placeholder: "Write a comment!" %>
    </p>
    <br>
    <p> <%= f.submit %> </p>

<% end %>

如果您需要查看更多代码或任何内容,只需发表评论并告诉我们,谢谢大家!

1 个答案:

答案 0 :(得分:1)

如果您在信息中心操作中致电raise params.inspect,则会发现没有此类属性为comment_id。然而,你的@comment定义试图调用一个。你根本不需要这个。

在您看来,您甚至没有引用@comment。至于添加发布评论的用户的用户名或图片,您必须将其放入评论部分views/comments/_comment.html.erb - 因为这是正在呈现的内容,每次评论一次。

某些人喜欢:

<div class="comment clearfix">
  <div class="comment_content">
    <p class="comment_body">
      <span class="commentor"><%= comment.user.name %></span>
      <span class="body"><%= comment.body %></span>
    </p>
    <% if @user_signed_in %>          
      <p><%= link_to "Delete", [comment.post, comment],
                    method: :delete, 
                    class: "button",
                    data: { confirm: "Are you sure?" } %>
      </p>
    <% end %>
  </div>
</div>
相关问题