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

編集:選択ボックスを使用して 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であると仮定します。ただし、このアプローチは非常に厄介であるとコメントしなければなりません。rablなどの json ビルダーを実際に使用する必要があります。

于 2013-10-16T04:39:54.003 に答える