如何在使用AJAX提交后删除创建审阅表单?

时间:2011-05-11 21:54:27

标签: javascript ruby-on-rails ajax

我正在创建一个用户可以在场地上撰写评论的应用,目前在场地展示页面上有一个添加评论表单。当用户添加评论时,它会使用AJAX更新页面并显示评论。重新加载页面时,它会识别当前用户已撰写评论但未显示添加新评论表单。

如何将其添加到AJAX功能中,以便在提交评论时自动删除添加新的评论表单?

场地show.html.erb

  <div id="reviews">
    <%= render :partial => 'reviews/review', :collection => @venue.reviews %>
  </div>

  <% unless reviewed? %>
    <%= form_for [@venue, @review], :class => 'rating_ballot' do |f| %>
      <%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
      <%= radio_button_tag("review[rating]", 1, :class => 'rating_button') %>

      <%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
      <%= radio_button_tag("review[rating]", 2, :class => 'rating_button') %>

      <%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
      <%= radio_button_tag("review[rating]", 3, :class => 'rating_button') %>

      <%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
      <%= radio_button_tag("review[rating]", 4, :class => 'rating_button') %>

      <%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
      <%= radio_button_tag("review[rating]", 5, :class => 'rating_button') %> <br>

      <p>title: <br>
      <%= f.text_field :title %></p><br>

      <%= submit_tag %>
    <% end %>
  <% end %>

查看_review.html.erb

<div class="review">

  <div class="reviewer_details">
    <div class="reviewer_details_photo"></div>
    <%= review.user.username %>
  </div>

  <div class="review_content">
    <h2 class="review_partial_title"><%= link_to review.title, [@venue, review] %></h><br>
    <p><%= review.rating %></p>
  </div>

  <div class="clearall"></div>
</div>

查看create.js.erb

$("#new_review").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
$("#reviews_count").html("<%= pluralize(@review.venue.reviews.count, 'Review') %>");
$("#reviews").append("<%= escape_javascript(render(:partial => @review)) %>");
$("#new_review")[0].reset();

感谢任何帮助,非常感谢!

1 个答案:

答案 0 :(得分:2)

在您的视图中,将表单包装在带有id的div中,例如“review_form”。

    <div id="reviews">
    <%= render :partial => 'reviews/review', :collection => @venue.reviews %>
    </div>
    <% unless reviewed? %>
    <div id="review_form">
    <%= form_for [@venue, @review], :class => 'rating_ballot' do |f| %>
      <%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
      <%= radio_button_tag("review[rating]", 1, :class => 'rating_button') %>

      <%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
      <%= radio_button_tag("review[rating]", 2, :class => 'rating_button') %>

      <%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
      <%= radio_button_tag("review[rating]", 3, :class => 'rating_button') %>

      <%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
      <%= radio_button_tag("review[rating]", 4, :class => 'rating_button') %>

      <%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
      <%= radio_button_tag("review[rating]", 5, :class => 'rating_button') %> <br>

      <p>title: <br>
      <%= f.text_field :title %></p><br>

      <%= submit_tag %>
    <% end %>
    </div>
  <% end %>

然后,在create.js.erb中,添加一些jquery来清除其内容,隐藏或一起删除它们。

$("#review_form").hide();