I have started using jQuery and rails. I have made a simple form that submits to the database and updates the page without reloading with ajax.
In my jQuery functions I used $"#new_comment" for the id name (in my application.js and create.js.erb files) but on my places/show view file, I named the div "add_comment" and it works..
I renamed it new_comment and it breaks! Can someone explain it please? I get an error: "no action responded to 1"
My function in my controller is "create".
#views/places/show.html.erb
<div id="new_comment" style="display:none">
#form goes here
</div>
#application.js
jQuery.fn.submitWithAjax = function(){
$("#new_comment").submit(function() {
$.post($(this).attr("action"), $(this).serialize(), null, "script");
return false;
})
};
$(document).ready(function() {
$("new_comment").submitWithAjax();
})
#create.js.erb
$("#new_comment").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
$("#comments_count").html("<%= pluralize(@comment.place.comments.count, 'suggestion') %>");
$("#comments").append("<%= escape_javascript(render(:partial => @comment)) %>");
$("#new_comment")[0].reset();
#comments_controller.rb
def create
@place = Place.find(params[:place_id])
@comment = @place.comments.create!(params[:comment])
flash[:notice] = "Thanks for your suggestion. Remember to share with friends!"
respond_to do |format|
format.html {redirect_to place_comments_path(@place)}
format.js
end
end