1

我需要在我的视图中显示一些元素,然后根据用户选择过滤它们。我试图将控制器结果中的变量与 javascript 变量进行比较,但在这种情况下似乎是不可能的。

我有以下模型:

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"
end

在我的控制器内部,我有这个来获得所有约会:

@appointments = Appointment.all

现在在我的视图中,我显示所有约会,如下所示:

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 %>
    ],

这很好用,但我有一个选项,用户可以选择一名医生,并且只显示该医生的预约。我怎样才能做到这一点?我试图像这样过滤它们,但它不起作用:

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 %>
    ],

谢谢你们。

4

2 回答 2

1

编辑:要使用选择框仅获取一位医生的预约,请在选择框中选择选项时首先发送 ajax 请求:

$("select").change(function() {
  $.ajax({
    type: 'POST',
    data: 'selected=' + $("select option:selected").text(),
    dataType: 'json',
    success: function(data) {
      if ($.trim(data) !== '') {
        // data should be the returned json of all the appointments for only the selected doctor. Do whatever you want with it.
      }
    },
    error: function(jqXHR, textStatus, errorThrown) {
      console.log(textStatus);
      console.log(errorThrown);
    },
    url: <insert post route here>
  })
});

这是控制器动作的样子:

def retrieve_doctor_id
  selected_doctor = Doctor.find(params[:selected])
  @appointments = selected_doctor.appointments.select([:id, :start_date, :end_date])
  respond_to do |format|
    format.json render :partial => "your_controller/retrieve_doctor_id", :locals => {:appointments => @appointments}
  end      
end

文件:app/views/your_controller/retrieve_doctor_id.json.erb:

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 %>
],

我还没有测试过,所以请告诉我它是否有任何错误或问题。

于 2013-10-16T04:03:12.977 回答
0

像这样的东西应该工作

events: 
    [
    <% @appointments.select { |a| a.doctor_id == @doctor_id }.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 %>
    ],

假设这@doctor_id是您要过滤的医生的 ID。但是,我必须评论说这种方法真的很混乱。你真的应该使用一个 json builder,比如rabl

于 2013-10-16T04:39:54.003 回答