如何在引导模式下创建和编辑书评

时间:2019-05-03 15:07:11

标签: ruby-on-rails ruby

我能够创建评论以正确的方式进行预订。但是我无法在模态中创建和编辑评论。

我可以在模式中加载新的审阅表单,但只有在书籍已包含审阅的情况下才加载。如果这些书没有评论,则模式按钮将不起作用。

我收到一个我不完全理解的错误

First argument in form cannot contain nil or be empty

指向编辑表单行

<%= form_for([@book, @review], remote: true) do |f| %>

我已经尝试为书籍模型实现模式,该模式可以正常工作,但评论却不能。

books_controller.rb

def show
  @reviews = @book.reviews
end

books / show.html.erb

<section id="reviews-section">
  <div class="review-form">
     <% if user_signed_in? %>
        <!-- Button trigger modal -->
        <button type="button" class="btn btn-secondary btn-sm" data-toggle="modal" data-target="#mynewreview">
                Write your review
        </button>
     <% else %>
         <p>You need to <%= link_to 'sign in', new_user_session_path, class: "btn btn-warning btn-sm" %> before writing a review buddy :)</p>
     <br>
     <% end %>
  </div>
  <br>
  <div id="reviews_panel">
      <%= render @reviews %>
  </div>
</section>

reviews / _review.html.erb

<% if user_signed_in? %>
   <% if review.user_id == current_user.id %>
      <div class="btn-group mr-2" role="group">
        <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#editreview_<%= review.id %>">Edit Modal</button>
         <%= link_to 'Delete Review', book_review_path(review.book, review), class: "btn btn-sm btn-danger", method: :delete, remote: true, data: { confirm: "Are you sure?" } %>
      </div>
   <% end %>
<% end %>

<%= render 'reviews/widgets/new_review_modal' %>
<%= render 'reviews/widgets/edit_review_modal' %>

评论/小工具/ _new_review_modal

<%= form_for([@book, @book.reviews.build], remote: true) do |f| %>
  <!-- Modal -->
  <div class="modal fade" id="mynewreview" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Review</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <div id="rating-form">
            <label>Rating:</label>
          </div>

          <div class="field">
            <p><%= f.text_area :comment, placeholder: 'Write comment...', class: "form-control review-text-field" %></p>
          </div>
        </div>
        <div class="modal-footer">
          <div class="actions">
            <%= f.button "Submit Review", class: "btn btn-success btn-sm", data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Submitting Review..."} %>
          </div>
        </div>
      </div>
    </div>
  </div>
<% end %>

<script>
    $('#rating-form').raty({
        path: '/assets/',
        scoreName: 'review[rating]'
    });
</script>

评论/小工具/ _edit_review_modal

<%= form_for([@book, @review], remote: true) do |f| %>
  <!-- Modal -->
  <div class="modal fade" id="editreview_<%= review.id %>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Edit your review</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <div id="rating-form">
            <label>Rating:</label>
          </div>

          <div class="field">
            <p><%= f.text_area :comment, placeholder: 'Write comment...', class: "form-control review-text-field" %></p>
          </div>
        </div>
        <div class="modal-footer">
          <div class="actions">
            <%= f.button "Submit Review", class: "btn btn-success btn-sm", data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Submitting Review..."} %>
          </div>
        </div>
      </div>
    </div>
  </div>
<% end %>

<script>
    $('#rating-form').raty({
        path: '/assets/',
        scoreName: 'review[rating]',
        score: <%= @review.rating %>
    });
</script>

1 个答案:

答案 0 :(得分:0)

在reviews_controller中>编辑操作,您还必须找到@book和@review。

def edit
  @review = @book.reviews.find(params[:id])
end

您似乎正在书的显示页面中显示所有评论。并且您想要使用ajax进行编辑。在这种情况下,您必须使用某种循环,并且必须将单个审阅对象分配给任何变量。您需要在表单对象中使用该变量

<%= @reviewes.each do |review| %>
  <% form_for [@book, review] ... do %>
    ...
  <% end %>
<% end %>