When I call $.getJSON, i want to reload tbody elements. In other words, I need to delete all elements and put new elements by Json response.
JS
$(document).ready(function(){
$('#position').sortable({
update: function(event, ui) {
var newOrder = $(this).sortable('toArray').toString();
$.getJSON('/save_positions.json', {ids:newOrder}, function(data){
??????
});
}
});
});
My View
# index.html.erb
<tbody id="position">
<%= render :partial => "activities"%>
</tbody>
Partial View
#_activities.html.erb
<% @activities.each do |activity| %>
<tr id='<%= activity.id %>'>
<td><%= activity.position %></td>
<td><%= link_to activity.id, activity_path(activity) %></td>
<td><%= activity.name %></td>
<td><%= activity.estimated %></td>
<td><%= activity.used %></td>
<td><%=l activity.created_at %></td>
</tr>
My Action (Application Controller)
def save_positions
@activities = Activity.all(:order => 'position')
respond_to do |format|
format.json { render :json => @activities }
end
end
From your code I get the impression that what you’re trying to do is generate new HTML server-side and insert it into the document. If that’s the case, try something like this:
If you really want to return JSON, then you’ll probably want to return an array of objects and iterate over it to generate new HTML, perhaps using a templating engine like Mustache, e.g.:
No template engine:
or
JS:
Mustache template: