I have page with many images, each of which has an associated text box used to submit tags on that image. Each image and text box is rendered through a partial view. Currently I am able to update tags and everything works correctly through a full page refresh. I would like to be able to do all this through AJAX, but I'm having some trouble with it.
Here is the form code in the partial view:
<%= image_tag image.src %>
<%= form_tag :controller => "images", :action => :update_tags, :remote => true, :image => image.id do %>
<td><%= label_tag "Tags:" %></td>
<td><%= text_field_tag :tags, image.tags %></td>
<td><%= submit_tag "Submit" %></td>
<% end %>
And the main form that calls the partial:
<% @images.each do |image| %>
<div id="image_#{image.id}">
<%= render :partial => "image_tag", :locals => { :image => image } %>
</div>
<% end %>
In the :update_tags method in the controller I add the image's tags to the DB and then want to reload that image's partial view.
Here is the controller's redirect code:
respond_to do |format|
format.js
format.html { redirect_to :back, :remote => true }
end
And the update_tags.js.erb:
$("#image_#{image.id}").html( "<%= escape_javascript(render :partial => 'image_tag', :locals => {:image => image} ) %>" );
It currently is working functionally, but no AJAX magic happening. I'm still learning Rails and jQuery, so any help would be greatly appreciated. I think I've included all relevant code, but let me know if there's anything else I should provide. Thanks!