找不到带有嵌套资源的'id'= 3的注释

问题描述 投票:0回答:1

我收到此错误:https://imgur.com/a/Mrvkm2Z我可以在mi课程帖子中创建评论,但由于此错误,我无法对其进行编辑]

我不知道如何解决这个问题,我到处都在寻找,但是没有什么可以帮助我解决问题

编辑这是我在评论控制器中的代码

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

  # GET /comments
  # GET /comments.json
  def index
    @comments = Comment.all
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
  end

  # GET /comments/new
  def new
    @course = Course.find(params[:course_id])
    @comment = @course.comments.build
  end

  # GET /comments/1/edit
  def edit
  end

  # POST /comments
  # POST /comments.json
  def create
    @course = Course.find(params[:course_id])
    @comment = @course.comments.build(comment_params)
    respond_to do |format|
      if @comment.save
        format.html { redirect_to @course, notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update
    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
        format.json { render :show, status: :ok, location: @comment }
      else
        format.html { render :edit }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  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
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:title, :body)
    end
end

所以我认为问题可能出在新建中,我不知道如何将模型与一对多联系起来,我能够在课程中创建注释,但是当我想编辑其中的1条注释时,错误,在我单击编辑的每条评论中,我都将我发送到具有相同错误id = 3的相同网址]

这也是我的html代码(也许错误也可能在那里:]]

<%= form_for @comment, :url => course_comments_path(params[:course_id]) do |f| %>
  <% if comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>
      <% comment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title, id: :comment_title %>
  </div>

  <div class="field">
    <%= f.label :body %>
    <%= f.text_area :body, id: :comment_body %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

和我的课程的show.html:

<% if current_user%>
<p id="notice"><%= notice %></p>

<p>
  <h1><%= @course.name %></h1>
</p>

<p>
    <h2>Comentarios del curso:</h2> 
</p>
<p>     
    <% @course.comments.each do |comment| %>

            <h3><%= comment.title %></h3>         
            <p><%= comment.body %></p>
            <%if current_user.admin%>
                <%= link_to 'Edit', edit_course_comment_path(@course) %>
            <%end%>
    <%end%>
</p>


<%if current_user.admin %>

    <%= link_to 'Postear', new_course_comment_path(@course)%>
    <%= link_to 'Edit', edit_course_comment_path%>
    <%= link_to 'Back',  authenticated_root_path %>


<%else%>
    <%= link_to 'Postear', new_course_comment_path(@course) %>
    <%= link_to 'Back',  authenticated_root_path %>
<%end%>

我收到此错误:https://imgur.com/a/Mrvkm2Z我可以在mi课程发布中创建评论,但由于此错误我无法对其进行编辑,我不知道如何解决此问题,我一直在寻找除了...

ruby-on-rails ruby
1个回答
0
投票

我认为这条线

© www.soinside.com 2019 - 2024. All rights reserved.