One feature I'm proud of -- turning a user's front-end button press into a back-end edit. It starts with this button press:
<div class="row">
<div class="patient">
<% if @patient.exercises.include?(exercise) %>
<button class="edit-exercise btn btn-warning" id='<%= exercise.id %>'>Remove</button>
<% else %>
<button class="edit-exercise btn btn-info" id='<%= exercise.id %>'>Add</button>
<% end %>
</div>
</div>
which this listener catches and fires a call that POSTs a little JSON to a custom, internal URL:
$(".edit-exercise").click(function(e){
e.preventDefault();
if (this.innerHTML == 'Remove') var action = 'remove'
if (this.innerHTML == 'Add') var action = 'assign'
sendData(this,action);
switchAddRemove(this);
e.stopPropagation();
return false;
});
});
function sendData(target,action){
path = window.location.href.split("/").slice(0,5).join("/")+"/"+action+"_exercise"
$.ajax({
url: path,
type: 'POST',
data: {ids: {exercise_id: target.id}},
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
});
}
which is a route in Ruby on Rails where the PostgreSQL is affected:
#special route to assign or unassign exercise to patient
def assign_exercise
patient_id = params[:id]
exercise_id = params['ids'][:exercise_id]
patient = Patient.find(patient_id)
exercise = Exercise.find(exercise_id)
if patient.exercises.include?(exercise)
render json: {
error: "This exercise already belongs to the patient",
status: 412 }, status: 412
else
patient.exercises << exercise
patient.save
render json: {}, status: 200
end
end
def remove_exercise
patient_id = params[:id]
exercise_id = params['ids'][:exercise_id]
patient = Patient.find(patient_id)
exercise = Exercise.find(exercise_id)
if patient.exercises.include?(exercise)
patient.exercises.delete(exercise)
patient.save
render json: {}, status: 200
else
render json: {
error: "This exercise already is absent from the patient",
status: 412 }, status: 412
end
end
Log in or sign up for Devpost to join the conversation.