【问题标题】:Ruby on Rails blog and adding comments to posts and editing and deleting commentsRuby on Rails 博客以及在帖子中添加评论以及编辑和删除评论
【发布时间】:2019-02-20 15:23:39
【问题描述】:

我正在写一个 ROR 博客,在此过程中遇到了一些问题。我目前正在学习 Rails,只是感觉完全迷失了连接所有部分。我已经在我的 cmets 部分工作了几天,终于能够在帖子上创建 cmets,但我无法编辑或删除它们。我还参考了下面的 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

cmets 控制器:

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/cmets 文件夹中的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>

我再次可以将 cmets 添加到帖子中。当我将鼠标悬停在评论上的编辑标签上时,我看到了这个:localhost:3000/cmets/72/edit

点击编辑时出现此错误

当我将鼠标悬停在删除按钮上时,我看到:localhost:3000/cmets/72

点击删除时出现此错误

我现在完全迷失了方向,我觉得我已经尝试了所有可能的方法,但似乎没有任何效果。请帮忙!这里也是 GitHub 存储库:https://github.com/angelr1076/rails-blog

【问题讨论】:

  • 我快速浏览了您的存储库,但这似乎不包括用于编辑 cmets 的链接。您能否检查一下您的最新更改是否已推送。
  • 嗨,迈克。我刚刚将我最近的更改推送到了 github。谢谢你看看这个! github.com/angelr1076/rails-blog

标签: ruby-on-rails ruby


【解决方案1】:

First argument in form cannot contain nil or be empty 告诉您&lt;%= form_for @comment do |f| %&gt; 中的@commentnil。这是因为在 CommentsControlleredit 操作中,您设置的是 @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

更新:

另外,根据您的代码,您应该将editdelete 链接更改为下面

<%= link_to("Edit Comment", edit_comment_path(c)) %>
<%= link_to("Delete Comment", comment_path(c), method: :delete)

您正在传递@post.id,这是一个帖子的ID。相反,您应该使用 comments.each 中的块变量传递评论的 id,注意这里不需要 .id,因为它可以被 Rails 推断出来。

【讨论】:

  • Pavan,我确实进行了这些更改,但是当我单击编辑或删除时,我仍然在堆栈跟踪中收到“无法找到带有 'id'=74 的评论”。
  • @Angel 检查我的更新答案。您也应该更改链接。
  • 成功了!令人惊讶的是,我花了一天多的时间试图弄清楚这一点,而你在几分钟内就搞定了。谢谢!
  • @Pavan 我想向 OP 解释他们看到的错误,以便他们将来能够找出类似的问题。由于您的答案已被接受,我刚刚编辑了您的答案,而不是发布新答案。我希望没关系。另外,我发现 OP 没有使用嵌套路由(还没有?)所以不应该在 CommentsController 中使用 params[:post_id] 查找帖子。
猜你喜欢
  • 2016-04-17
  • 2014-08-19
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多