Sunday, 17 August 2014

How to use $.get to populate a using a Rails query



I have some javascript that displays a list of members(links in this case) for that group.

How can I get rails to populate that list?


I can currently get a list of the members in group 45 in a browser with a html view using /groups/45


I am trying a $.get call as shown but I am not sure how to get results into a list. Presumably via xml or json but not sure how.


My view has:



%ul#sortable
- @members.each do |loop|
= content_tag_for :li, loop do
- construct_hyperlink(loop.url_address, loop.alt_text)
= sanitize @address_url
\- #{h @new_link_alt_text}   #{link_to 'details', link_path(loop), {"title" => loop.alt_text}}   #{link_to 'edit', edit_link_path(loop), {"title" => loop.alt_text}}


and my js has:



$(function(){
$("a[data-show-group-members]='true'").bind('click', function() {
var gid= $(this).data("id");
var gname= $(this).data("name");
var links=$.get('/groups/' + gid);
$('<tr><td colspan="4">Members:</br><ul><li>1</li><li>2</li><li>3</li></ul>Delete the <b>'+gname+'</b> group and all its members: <b><a rel="nofollow" data-method="delete" data-confirm="Are you sure?" href=\'/groups/'+gid+'\'>Yes</a></b> <b><a href="groups" data-close-group="true">No</b></br></br>').insertAfter($(this).closest('tr'));
$(this).html("");
});
});


but I am not sure how to populate that UL (currently it has placeholder li's


My groups controller has:



def show
@group = Group.find(params[:id])
@members = Link.find_all_by_group_id(params[:id], :order => 'position')
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @group }
end
end


so I could use the format.xml or I could add a format.json but how do I call that and output a UL that has the group names.


Group is:



id: integer, group_name: string,
created_at: datetime, updated_at: datetime, group_description: string


After I display the members and a link to delete the group, that part works ok.

This is just about listing the members (links) of that group.


I prefer answers that avoid frameworks (other than rails and jquery) for educational purposes.


No comments:

Post a Comment