Within my Index view, I have a table that I want to dynamically populate with an ID. This is what I've attempted so far:
id="table_<%= @controller_name %>"
The method in my controller looks like this:
def get_controller_name
@controller_name = self.class.name.split("::").last
end
Next, I need to access a specific table in my Coffeescript. Here's how I've done it:
$ ->
myvar = '<%= raw @controller_name.to_json %>'
myvarAsObj = JSON.parse(myvar)
$('#' + 'table_' + myvarAsObj).DataTable
However, it doesn't seem to be working as expected. When I check the Page Source, the Table ID appears as follows:
id="table_MyController"
Could you please advise on the correct way to access the table ID in Coffeescript? Thank you!
Update:
This is the table in my Index:
<table data-controller-name="<%= @controller_name %>" cellpadding="0" cellspacing="0"
border="0" class="table table-striped table-bordered table-hover" width="100%"
data-source="<%= campaign_campaigns_index_path(format: :json) %>">
Here's the Coffeescript code:
$ ->
$('table[data-controller-name]').each ->
$(this).DataTable
ajax: $('table[data-controller-name]').each ->
$(this).data('source')
When I look at the Page Source, I see the following:
<table data-controller-name="CampaignsController" cellpadding="0" cellspacing="0" border="0"
class="table table-striped table-bordered table-hover" width="100%"
data-source="/en/campaigns.json">