提交#show中的NoMethodError

时间:2018-02-08 02:23:07

标签: ruby-on-rails undefined

我收到以下错误:

  提交#show中的

NoMethodError   未定义的方法`comment_path'

我可以登录,输入标题和网址,发表评论。当我点击“创建评论按钮”

这是错误的地方:

<%= link_to 'delete', comment, method: :delete, data: { confirm: 'are you sure?'} %>

除了错误之外,我正在尝试添加一条评论而不是删除一条评论?

_comment.html.erb

 <%= div_for(comment) do %>
     <div class="comments-wrapper clearfix">
       <div class="null-left">
          <p><small> <%= comment.user.email %> <strong><%= 
          time_ago_in_words(comment.created_at) %></strong> ago </small></p>
          <p class="lead"><%= comment.body %></p>
       </div> 
     </div> 
    <% if comment.user == current_user %> 
        <%= link_to 'delete', comment, method: :delete, data: { confirm: 
            'are you sure?'} %> 
        <% end %>
    <% end %>

comments_controller.rb

class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]

   def create
    @submission = Submission.find(params[:submission_id])
    @comment = @submission.comments.new(comment_params)
    @comment.user = current_user

respond_to do |format|
  if @comment.save
    format.html { redirect_to @submission, notice: 'Comment was successfully created.' }
  else
    format.html { render :new }
  end
end
  end
      def destroy
        @comment.destroy
    respond_to do |format|
      format.html { redirect_to comments_url, notice: 'Comment was 
        successfully destroyed.' }
    format.json { head :no_content }
    end
  end

  private

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


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

submission_controller.rb

    class SubmissionsController < ApplicationController
      before_action :set_submission, only: [:show, :edit, :update, :destroy]
      before_action :authenticate_user!, except: [:index, :show]

      def index
    @submissions = Submission.all
  end

  def show
  end

  def new
    @submission = current_user.submissions.build
  end

  def edit
  end

 def create
    @submission = current_user.submissions.build(submission_params)

    respond_to do |format|
      if @submission.save
        format.html { redirect_to @submission, notice: 'Submission was 
          successfully created.' }
        format.json { render :show, status: :created, location: @submission 
          }
      else
        format.html { render :new }
        format.json { render json: @submission.errors, status: 
           :unprocessable_entity }
      end
    end
  end

 def update
    respond_to do |format|
      if @submission.update(submission_params)
        format.html { redirect_to @submission, notice: 'Submission was 
   successfully updated.' }
        format.json { render :show, status: :ok, location: @submission }
      else
        format.html { render :edit }
        format.json { render json: @submission.errors, status: 
        :unprocessable_entity }
      end
    end
  end

def destroy
@submission.destroy
respond_to do |format|
  format.html { redirect_to submissions_url, notice: 'Submission was 
  successfully destroyed.' }
  format.json { head :no_content }
   end
  end

def upvote
    @submission = Submisson.find{params[:id]}

    @submission.upvote_by current_user
    redirect_to :back
  end

  def downvote
    @submission = Submisson.find{params[:id]}

    @submission.downvote_by current_user
    redirect_to :back
  end

  private

def set_submission
  @submission = Submission.find(params[:id])
end


def submission_params
  params.require(:submission).permit(:title, :url)
end
end

的routes.rb

Rails.application.routes.draw do

  devise_for :users
    resources :submissions do
      member do
        put "Like", to: "submission#upvote"
        put "Dislike", to: "submission#downvote"
    end
    resources :comments 
  end
  root to: "submissions#index"


end

1 个答案:

答案 0 :(得分:0)

对于删除,您需要传递提交ID,如

您的代码

<%= link_to 'delete', comment, method: :delete, data: { confirm: 'are you sure?'} %>

修改后

<%= link_to 'Delete', [comment.submission, comment], method: :delete, data: { confirm: 'Are you sure?' } %>

在评论表上

<%= f.hidden_field :user_id, value: current_user.id %>

在评论控制器上创建操作,删除这两行

@comment = @submission.comments.new(comment_params)
@comment.user = current_user

更新后删除链接然后测试评论创建如果不创建评论仍然按照以下代码。

现在你的评论创建动作看起来像这样

def create
    @submission = Submission.find(params[:submission_id])
    respond_to do |format|
      if @submission.comments.create(comment_params)
        format.html { redirect_to @submission, notice: 'Comment was successfully created.' }
      else
        format.html { render :new }
      end
    end
end

我的项目中的代码已经正常工作,如果没有工作,那么如果有你的公共存储库,则发布URL或发布完整的错误跟踪。

基于GitHub链接更新

如果你运行rake routes,那么你会看到评论删除路由格式,如

DELETE /submissions/:submission_id/comments/:id(.:format)      comments#destroy

您需要通过submission_idid进行评论删除

更新link_to删除看起来像这样

<%= link_to 'Delete', [comment.submission, comment], method: :delete, data: { confirm: 'Are you sure?' } %>

您的destroy操作在comments_controller

上显示如下
def destroy
    @submission = Submission.find(params[:submission_id])
    @comment = @submission.comments.find(params[:id])
    @comment.destroy
    redirect_to submission_path(@submission)
end

希望有所帮助