2013-10-16 37 views
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 %> 
    ], 

完美的作品,但我有一个选项,用户可以选择一个医生,只有appoi应该出现该医生的资料。我怎么能做到这一点?我试图像这样过滤它们,但它不起作用:

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

谢谢大家。

回答

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 

文件:应用程序/视图/ 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 %> 
], 

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

+0

谢谢,但不起作用,因为doctor_id是一个javascript变量,我在用户从下拉菜单中选择一个选项后创建。我收到以下错误:“未定义的本地变量或方法'doctor_id'” – Cristiano

+0

@ ZdravkoVajudin我认为比较JavaScript变量和ruby/rails变量的唯一方法是在选择框更改后发生ajax post请求,然后转到一个控制器,该控制器将JavaScript的约会'doctor_id'发送回去。我会用我正在谈论的内容更新我的答案。 – jvperrin

+0

@ZdravkoVajudin我已经更新了我的答案,希望它能更好地工作。 – jvperrin

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构建器,如rabl

+0

谢谢,但问题是,当用户从下拉菜单中选择一个选项时,doctor_id被创建,所以它是一个JavaScript变量,我不能这样比较。 – Cristiano

相关问题