How can I remove a create review form after submitting using AJAX?
I'm making an app where users can write reviews on venues and currently have an add review form on the venues show page. When a user adds a review it updates the page with AJAX and displays the review. When the page is reloaded it recognises that the current user has written a review and doesn't show the add new review form.
How can I add that into the AJAX function so that is automatically removes the add new review form on submitting a review?
venue 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 _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><开发者_如何学C;%= review.rating %></p>
</div>
<div class="clearall"></div>
</div>
review 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();
Thanks for any help its much appreciated!
In your view, wrap the form in a div with an id, say "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 %>
Then, in create.js.erb, add some jquery to clear its contents, hide, or remove it all together.
$("#review_form").hide();
精彩评论