代码之家  ›  专栏  ›  技术社区  ›  JackJack

rubyonrails博客,为文章添加评论,编辑和删除评论

  •  0
  • JackJack  · 技术社区  · 7 年前

    我正在写一个ROR博客,在写博客的过程中遇到了一些问题。我目前正在学习Rails,只是觉得完全失去了连接所有部件的能力。我已经在我的评论部分工作了几天,终于可以在帖子上创建评论了,但是我不能编辑或删除它们。我也提到了下面的问题,但仍然遇到问题。

    注释模型参数:

    模型关联:

    user.rb
    
    has_many :posts
    has_many :comments
    
    post.rb
    
    belongs_to :user
    has_many :comments
    
    comment.rb
    
    belongs_to :user
    belongs_to :post
    

    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
    

    帖子显示.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>
    

    <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/评论/72/编辑

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

    error when trying to edit a comment

    当我将鼠标悬停在“删除”按钮上时,会看到:本地主机:3000/comments/72

    error when trying to delete a comment

    https://github.com/angelr1076/rails-blog

    1 回复  |  直到 7 年前
        1
  •  0
  •   mikej heading_to_tahiti    7 年前

    这个 First argument in form cannot contain nil or be empty 他在告诉你 @comment <%= form_for @comment do |f| %> nil . 这是因为 edit CommentsController 您正在设置 @post @评论 .

    将此更改为:

    def edit
      @comment = Comment.find(params[:id])
    end
    

    如果要删除注释 Couldn't find Comment without an ID find . 这是因为你想用 params[:comment_id] params[:id] . 将销毁操作更改为:

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

    更新:

    编辑 delete 链接到下面

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

    你路过了 @post.id 它是post的id。相反,您应该使用来自 comments.each ,注意到 .id

    推荐文章