Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tabishiqbal
Created June 6, 2017 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabishiqbal/583d4ab9d3db24b4b8bd4c98fc3f1e51 to your computer and use it in GitHub Desktop.
Save tabishiqbal/583d4ab9d3db24b4b8bd4c98fc3f1e51 to your computer and use it in GitHub Desktop.
render record data via ajax
<div class="col-md-8">
<div class="ibox-title"><h4>Employees</h4></div>
<div class="ibox-content">
<table id="employee-table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Barcode</th>
</tr>
</thead>
<tbody>
<% @employees.each do |employee| %>
<tr class="empRow">
<td><%= employee.id %></td>
<td><%= employee.first_name %></td>
<td><%= employee.last_name %></td>
<td><%= employee.barcode %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
$(document).on('click', '.empRow', function(){
var empId = $(this).children('td').html(); //gets emp id
$.get('/employees.js?id=' + empId).success(function(){
// $('#issuCard').css({'visibility':'visible'});// show card
});
});
<!-- Card showing Information using Ajax -->
<div id="empCard">
<div class="col-md-4">
<div class="ibox-title"><h4><%= @employee.first_name %> <%= @employee.last_name %></h4></div>
<div class="ibox-content">
<label for="">Employee ID: </label> <label><%= @employee.id %></label> <br>
<label for="">Employee Barcode: </label> <label><%= @employee.barcode %></label> <br>
<%= image_tag @employee.avatar.url(:thumb) %>
<hr>
<div class="row">
<div class="col-md-6">
<%= button_to 'View', employee_path(@employee.id), :class=>'btn btn-info btn-sm btn-block', :method => :get %>
</div>
<div class="col-md-6">
<%= button_to 'Edit', edit_employee_path(@employee.id), :class=>'btn btn-default btn-sm btn-block', :method => :get %>
</div>
</div>
</div>
</div>
</div>
<!-- EMPLOYEE INDEX -->
<div class="row">
<div class="col-sm-8">
<div class="pull-right"><%= link_to 'Add Employee', new_employee_path, class: "btn btn-w-m btn-primary" %></div>
</div>
</div><br>
<div class="row">
<%= render 'employees' %>
<% if !!@employee %>
<%= render 'employee_card'%>
<% end %>
</div>
<br>
$("#empCard").html("<%= j(render(:partial => 'employee_card')) %>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment