ruby on rails - How to replace a div tag in my view using Ajax -


i want replace div tag on images index page using ajax. right now, have following:

_sub.html.erb

<% @group.images.each |image| %>       <%= image_tag image.pic.url(:medium) %>       <%= button_to '+1', image_upvote_path(image), remote: true, method: :post %> <% end %> 

index.html.erb

<div id="next_group">     <%= render 'sub' %> </div> 

upvote.js.erb

$("#next_group").empty(); $("#next_group").append("<%= j render 'sub' %>"); 

in images_controller

def index     @group = group.offset(rand(group.count)).first end  def upvote     @image = image.find(params[:image_id])     @image.votes.create      respond_to |format|         format.html { redirect_to groups_path  }         format.js     end end 

and in routes, have

image_upvote post   /images/:image_id/upvote(.:format) images#upvote 

my understanding of going on:

on index page have div container renders sub partial want users see. inside sub partial, have button_to helper has remote: true attribute(?) included path/action clicking button initiate. action images#upvote. in images controller, define want happen when button clicked (an upvote created), want respond ajax, happens because i've declared remote: true.

here's start getting little confused. since ajax being used, rails automatically upvote.js.erb file since it's upvote action occurring?

my problem right vote being created fine, don't think javascript in upvote.js.erb being executed. page stay on current @group being displayed, without rendering new _sub partial. guess i'm not sure if there problem javascript, or maybe way have controller, views, , routes set up.

maybe button not belongs form, try using link_to or add remote options path


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -