【问题标题】:I cannot make ajax requests with Ruby我不能用 Ruby 发出 ajax 请求
【发布时间】:2020-10-25 12:47:34
【问题描述】:

我想通过 Ajax 请求在 Rails 上制作 cmets。我为此添加了一些代码,最终遇到了这样的错误。

Missing partial comments/_comment with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
  * "C:/Users/adilc/Desktop/Ruby Dersleri/Book-Sharing/app/views"
  * "C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/devise-4.7.3/app/views"
  * "C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actiontext-6.0.3.4/app/views"
  * "C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actionmailbox-6.0.3.4/app/views"

提取的源代码(第 63 行附近):

<% end %>
    
<%= render @book.comments %> (ERROR IN THIS LINE)
<div class="comments"></div>
<h4 style="margin-top: 5px;" align="center"> Comments </h4>

我的代码;

app/controllers/cmets_controller.rb

    
    class CommentsController < ApplicationController
    
        def create
    
            if current_user
    
                @book = Book.find(params[:book_id])
                @comment = @book.comments.create(comment_params)
                @comment.user_id = current_user.id
                @comment.approve = false
                @comment.save
                
    
                respond_to do |format|
                    format.js
                end
            end
    
        end
    
        def show
    
            @comment = Comment.find(params[:id])
    
        end

app/views/cmets/show.html.erb

<div class="comments">

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

  <p>
    <h4><%= @comment.book.title %> Book Comment</h4>
  </p>

  <br>

  <p>
    <strong>Title:</strong>
    <%= @comment.title %>
  </p>

  <p>
    <strong>Content:</strong>
    <%= @comment.body %>
  </p>

  <p>
    <strong>Commenter:</strong>
    <%= link_to @comment.user.username, @comment.user %>
  </p>

  <p>
    <%= link_to 'Approve It', @comment_path, method: :patch, class: 'btn btn-outline-primary' %>
  </p>
  <br>

app/views/cmets/create.js.erb

$("#flash_notice").html("Your review is under review for approval");

$(".comments").append("<%= escape_javascript(render(@comment))%>");

app/views/books/show.html.erb

<div class="book_info">


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

  <p>
    <strong>Title:</strong>
    <%= @book.title %>
  </p>

  <p>
    <strong>Author:</strong>
    <%= @book.author %>
  </p>

  <p>
    <strong>Page Count:</strong>
    <%= @book.pagecount %>
  </p>

  <p>
    <strong>User:</strong>
    <%= link_to @book.user.username, @book.user %>
  </p>

  <p>
    <strong>Tradable:</strong>
    <% if @book.tradeable %>
      Open to sharing
    <% else %>
            Not shared
    <% end %>
  </p>


  <% if @book.user == current_user %>
  <%= link_to 'Edit', edit_book_path(@book), class: 'btn btn-outline-primary', style: 'margin-bottom: 5px;' %>
  <% end %>
  <% if current_user %>
    <div class="flash_notice"></div>
    <h4> Add a comment:</h2>
    <%= form_with(model: [@book, @book.comments.build], local: false) do |form| %>

      <p>
        <%= form.label :title %><br>
        <%= form.text_field :title, class: 'form-control', style: 'width: 50%' %>
      </p>

      <p>
        <%= form.label :body %><br>
        <%= form.text_area :body, class: 'form-control', style: 'width: 50%' %>
      </p>

      <p>
        <%= form.submit %>
      </p>

    <% end %>

  <% end %>
  <h4 style="margin-top: 5px;" align="center"> Comments </h4>
  <%= render @book.comments %>
  <div class="comments"></div>

如何解决这个错误或以不同的方式抛出 ajax。

【问题讨论】:

    标签: ruby-on-rails ruby ajax


    【解决方案1】:

    您遇到的错误是告诉您缺少包含您引用的位置的文件。

    在你的 js 中

    $(".comments").append("<%= escape_javascript(render(@comment))%>");
    

    您没有提供位置,因此 rails 会根据对象类别为您确定位置。

    所有部分(即从 js 转义中呈现的内容)将在文件名的开头有一个下划线。而且由于对象类型是Comment - 它试图找到文件

    comments/_comment.html.erb
    

    您可以通过将show.html.erb 移至_comment.html.erb 来解决此问题

    【讨论】:

    • 谢谢它的工作,但有这样的问题。我将 _comment.html.erb 中的 cmets 拉到每个位置,但是当我在屏幕上按 show.html.erb 时,无论我有多少 cmets,show.html.erb 都会循环与 cmets 的数量一样多,并且它会出现一条评论在多个屏幕上显示。
    • 真正应该在 _cmets 中的唯一部分是评论表单和显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多