I am wondering if i can use rails tags in html using jquery to reload a table based on a new query. See below:
<table id = "PlyrsTm2" style = float:right>
<tr><th id="PTTitle" colspan=2>List of players on selected team</th></tr>
<tr><th id="PTHRows">Player</th><th id="PTHRows">Team</th></tr>
<% @pl.each do |f| %>
<tr><td class="player"><%= f.Plyr %></td><td class="team"><%= f.Team %></td></tr>
<% end %>
</table>
So i know that i can do this:
$('#PlyrsTm2').html('<table id = "PlyrsTm2" style = float:right><tr><th id="PTTitle" colspan=2>List of players on selected team</th></tr><tr><th id="PTHRows">Player</th><th id="PTHRows">Team</th></tr><tr><td class="player"></td><td class="team"></td></tr></table>
but if I add the rails part to it, then it does not refresh/reload the table. I have looked all over the internet and have not found the solution. I posted a question similar to this, but no one answered and it was voted down because of not enough research(which is not true) because i only post questions to answer that i do not find so can someone help me with this, i would greatly appreciated
RE-UPDATED
it might be helpful to post the ajax and jquery that i am using:
$(document).ready(function(){
$('#showbtn').on('click', function() {
ids = $('#teams').val()
IM = false
$.ajax({
url: "http://localhost:3000/teamplayers.json?resolution="+ids+"&import="+IM,
type:"get",
dataType: "json",
cache: true,
success:function(data){
$('#PlyrsTm2').html(data);
$('#PlyrsTm2').show();
alert(data);
alert("Loading Players....");
},
error: function(error) {
alert("Failed " + console.log(error) + " " + error)
}
});
$('#PlyrsTm2').trigger('create');
});
});
I click a button (#showbtn) which it passes a integer value (#teams) to my controller, which is then changed and here is the table i want it to load:
'<table id = "PlyrsTm2" style = float:right><tr><th id="PTTitle" colspan=2>List of players on selected team</th></tr><tr><th id="PTHRows">Player</th><th id="PTHRows">Team</th></tr><tr><% @pl.each do |f| %><td class="player"><%= f.Plyr %></td><td class="team"><%= f.Team %></td></tr><% end %></table>
As you can see there is Rails html in it, but i do not think it actually re-excutes the rails part <& @pl.each do |f| %>
which then i see the same table that loaded when the page was first opened. So how do i get the rails to re-execute with the new info?
I can post the controller method if needed.
UPDATE Here is the controller
class TeamplayersController < ApplicationController
before_filter :set_id
before_action :set_id, :set_teamplayer, only: [:show, :edit, :update, :destroy]
# GET /teamplayers
# GET /teamplayers.json
def index
@teamplayers = Teamplayer.all
@fteams = Fteam.all
tid = params[:resolution]
toimport = params[:import]
if tid.nil?
tid = 1
else
tid = tid.to_i;
end
@ids = tid
@pl = Teamplayer.joins(:live_player).where(:teamid => @ids).all << Here should refresh the table
if toimport == "true"
@turl = Fteam.where(:id => @ids).pluck(:TeamUrl)
@turl = @turl[0]
system "rake updateTm:updateA[#{@turl},#{@ids}]"
end
end