Ruby on Rails博客,并在帖子中添加评论,以及编辑和删除评论

时间:2018-09-16 14:47:36

标签: ruby-on-rails ruby

我正在ROR博客上工作,并且在此过程中遇到了一些问题。我目前正在学习Rails,只是对连接所有部件感到完全迷失。我已经在“评论”部分上工作了几天,终于能够在帖子中创建评论,但是我无法对其进行编辑或删除。我还在下面引用了SO问题,但仍然遇到问题。

这是我的布局:

评论模型参数:

body \ user_id \ post_id

模型关联:

user.rb

has_many :posts
has_many :comments

post.rb

belongs_to :user
has_many :comments

comment.rb

belongs_to :user
belongs_to :post

routes.rb:

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  get '/' => 'users#index'

  get '/posts' => 'posts#index'

  post '/posts/create' => 'posts#new'

  post '/posts/edit' => 'posts#edit'

  get '/signin' => 'sessions#new', as: :new_session
  post '/create-session' => 'sessions#create', as: :create_session
  get 'signout' => 'sessions#destroy', as: :destroy_session

  resources :users

  resources :posts
  resources :comments
end

评论控制器:

class CommentsController < ApplicationController

  def index
    @comment = Comment.all 
  end

  def new
    user = session[:user_id]
    @comment = Comment.new(post_id: params[:post_id])
    @post = Post.find(params[:post_id])
  end

  def create
    @comment = Comment.new(comment_params)
    @comment.user_id = session[:user_id]
    @postid = params[:id]
    if @comment.save
      flash[:notice] = "comment created."
      redirect_to '/posts'
    else
      flash[:error] = "Error creating comment."
      redirect_to '/posts'
    end
  end

  def edit
    @post = Post.find(params[:id])
  end

  def update
    @comment = Comment.find_by_id(params[:id])
    @comment.update(comment_params)
    flash[:notice] = "Comment updated."
    redirect_to '/posts'
  end

  def destroy
    @comment = Comment.find(params[:comment_id])
    @comment.destroy
    redirect_to '/posts'
  end

  private 

    def comment_params
      params.require(:comment).permit(:body, :user_id, :post_id)
    end
  end

在views / posts文件夹中发布show.html.erb页面:

<%# show all posts %>

<div id="single-post">
    <h1>User - <%= @post.user.username %></h1>

        <h2>Post -  <%= @post.body %> </h2>

        <%= link_to("Edit Post", edit_post_path(@post)) %>
        </br>
        <%= link_to("Delete Post", @post, method: 'delete') %>
        </br>
        <%= link_to("Add Comment", new_comment_path(post_id: @post.id)) %>
        <%#<%= link_to("Edit Comment", edit_comment_path(post_id: @post.id, comment_id: @comment.id))%>
</div>


    <h3><% @post.comments.reverse.each do |c| %> </h3> 
        <div id="single-comment">
            <h4>Comment</h4>
            <h5>From - <%= c.user.username %></h5>

            <h6><%= c.body %> </h6>

            </br>
            <%= link_to("Edit Comment", edit_comment_path(@post.id)) %>
            </br>
            <%= link_to("Delete Comment", comment_path(@post.id), method: :delete) %>

        </div>
    <% end %>

</div>

views / comments文件夹中的new.html.erb表单

<div id="comment-form">
    <%= form_for @comment do |f| %>
        <%= f.label :body %>
        <%= f.text_area :body, class: "text-area" %>
        <%= f.hidden_field :post_id %>
        <%= f.submit %>
    <% end %>
</div>

同样,我可以在帖子中添加评论。当我将鼠标悬停在评论的edit标签上时,我看到的是:localhost:3000 / comments / 72 / edit

单击“编辑”时,我会看到此错误

error when trying to edit a comment

当我将鼠标悬停在删除按钮上时,我看到以下内容:localhost:3000 / comments / 72

当我点击删除时,我会看到此错误

error when trying to delete a comment

我正处于完全迷失的境地,觉得我已经尝试了所有可能的方法,但似乎无济于事。请帮忙!这也是GitHub存储库:https://github.com/angelr1076/rails-blog

1 个答案:

答案 0 :(得分:0)

First argument in form cannot contain nil or be empty告诉您@comment中的<%= form_for @comment do |f| %>nil。这是因为在edit的{​​{1}}操作中,您设置的是CommentsController而不是@post

更改为:

@comment

要删除评论,def edit @comment = Comment.find(params[:id]) end 告诉您要传递给Couldn't find Comment without an ID的值为find。这是因为您尝试使用nil而不是params[:comment_id]。将销毁动作更改为:

params[:id]

更新

还要根据您的代码,将def destroy @comment = Comment.find(params[:id]) @comment.destroy redirect_to '/posts' end edit链接更改为以下

delete

您正在传递<%= link_to("Edit Comment", edit_comment_path(c)) %> <%= link_to("Delete Comment", comment_path(c), method: :delete) ,它是帖子的ID。相反,您应该使用@post.id中的block变量传递注释的ID,请注意这里不需要comments.each,因为它可以由Rails推断。