I am facing a challenge where I need to display certain elements in my View and then filter them based on user selection. I attempted to compare variables from the controller result with a JavaScript variable, but unfortunately, it seems that this approach is not feasible in this particular situation.
Here is the model I am working with:
create_table "appointments", force: true do |t|
t.integer "doctor_id"
t.integer "user_id"
t.datetime "start_date"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "end_date"
Within my controller, I retrieve all appointments using the following code:
@appointments = Appointment.all
Next, in my View, I display all appointments as follows:
events:
[
<% @appointments.each do |appointment| %>
{
id : "<%= appointment.id %>",
title : "Reserved",
start : "<%= appointment.start_date.in_time_zone.strftime '%Y-%m-%dT%H:%M:%S' %>",
end : "<%= appointment.end_date.in_time_zone.strftime '%Y-%m-%dT%H:%M:%S' %>",
allDay : false
},
<% end %>
],
While this setup works perfectly fine, I now have a requirement where users can select a specific doctor, and only appointments associated with that doctor should be displayed. How can I achieve this? I tried implementing a filter like this, but it does not seem to work:
events:
[
<% @appointments.each do |appointment| %>
if <%= appointment.doctor_id %> == doctor_id{
{
id : "<%= appointment.id %>",
title : "Reserved",
start : "<%= appointment.start_date.in_time_zone.strftime '%Y-%m-%dT%H:%M:%S' %>",
end : "<%= appointment.end_date.in_time_zone.strftime '%Y-%m-%dT%H:%M:%S' %>",
allDay : false
},
}
<% end %>
],
Any advice or guidance on how to tackle this issue would be greatly appreciated.