Looking for guidance on passing data through ajax to the controller. The page where I have this setup is /admin/projects/report_project_resources.html.erb
<%= select("project", "report", Project.where(closed: false).collect { |p| [ p.name, p.id ] }, { prompt: "Seleziona un piano formativo" }) %>
<div id="hiddenField" style="display: none">
<div class="row">
<div class="col-xs-12">
<div style="margin: 20px 20px;">
<%= link_to 'Genera report', generate_project_resource_associations_admin_projects_path, :class => "btn btn-default" %>
</div>
<div class="box">
<!-- /.box-header -->
<div class="box-body" id="table-box">
<%= render :partial => "table" %>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
</div>
Upon selecting a project from the list, the partial /admin/projects/_table will be displayed using the script below:
$("#project_report").change(function(){
if ($(this).val() !== '') {
$("#hiddenField").show();
var project_id = $(this).val();
$.ajax({
type: "GET",
url: "/admin/projects/report_project_resources.js",
data: {
projectId: project_id
}
});
console.log(data);
}
else if ($(this).val() === '') {
$("#hiddenField").hide();
}
});
I have created a /admin/projects/report_project_resources.js.erb file with:
$('#table-box').html("<%= escape_javascript (render partial: 'table') %>");
To load the partial content. In my current partial _table file, I am testing the passed params with:
<h1><%= params[:projectId] %></h1>
To troubleshoot why I can't access the project found by @project variable in the controller. In /admin/projects/projects_controller.rb:
def report_project_resources
@project = params[:projectId]
project = Project.find_by(id: @project)
end
If anyone could provide assistance or guidance, it would be greatly appreciated. Thank you.