【问题标题】:Filter events in calendar for ruby on rails application为 ruby​​ on rails 应用程序过滤日历中的事件
【发布时间】:2013-11-25 15:04:34
【问题描述】:

我已经使用fullcalendar assets gem 列出日历中的所有事件,我尝试根据我的需要修改日历,但对于这种情况,我感到困惑。 我用过https://github.com/bokmann/rails3_fullcalendar

场景: 我必须过滤掉日历中特定工作人员的所有约会。所以在我的约会控制器中,我以这种方式列出了所有约会。

@appointments = @client.appointments

它列出了客户的所有约会,所以现在我要根据我列出客户所有工作人员的下拉按钮的选择再次过滤掉约会。因此,根据所选的工人,它将显示该特定所选工人的约会。

代码尝试

1. Rails 方式

首先,我尝试使用 rails 方式,我需要将工作人员的 id 从下拉列表传递到控制器,我使用的下拉代码:

<%= form_tag appointments_path do %>
    <%= select_tag(:worker_id, options_from_collection_for_select(@client.workers, :id, :alias), :include_blank =>true)%>
    <%=submit_tag "Display"%>
<% end %>

但我不知道在控制器中传递什么来获取记录。

2。 JS方式

<%= select_tag(:worker_id, options_from_collection_for_select(@client.workers, :id, :alias), :include_blank =>true),:onchange => 'a(this.value)'%>

calendar.js

function a(pointer){
var intWorkerId = pointer;
alert(intWorkerId);
$.ajax({
    url:'/appointments',
    dataType: "json",
    data:({
            id:intWorkerId
        }),
    type:'get',
    success:function(result){

        alert(result);
        $('#content').html(result);
                console.log(1);
  }
});
}

当我尝试打印值并尝试从该 id 打印值时,我如何在控制器中获取工人的 id,这是正确的,但我不知道如何继续。

Appointment.rb

def as_json(options = {})
{
  :id => self.id,
  :title => self.title,
  :description => self.description || "",
  :start => appointment_start_time.rfc822,
  :end => appointment_end_time.rfc822,
  :allDay => self.all_day,
  :recurring => false,
  :url => Rails.application.routes.url_helpers.appointment_path(id)
  #:color => "red"
}

Appointment_controller.rb

if params[:id]
  #@appointments = @client.workers.params[:id].appointments
  #p "*********id values****#{params[:id]}"
  #worker = Worker.find(params[:id])
  #p"***** #{worker.alias}"
  #@appointments = worker.appointments
  #p "******* #{@appointments.count}"
  p "*******hello id is here*******"
else
  @appointments = "hello"
  p "*******hello id is not here*******"
end  

我已经尝试了所有可能的组合,但无法获得结果。请帮帮我。

谢谢

【问题讨论】:

    标签: javascript ruby-on-rails ruby ruby-on-rails-3 fullcalendar


    【解决方案1】:

    如果约会是 Worker 上的关联,您可以像这样获取客户 Worker:

    @worker = @client.workers.find(params[:id])
    

    得到那个工人的约会:

    @appointments = @worker.appointments
    

    这是假设在您的 Appointment 课程中您有:

    class Appointment < ActiveRecord::Base
      belongs_to :worker
    end
    

    还有你的工人:

    class Worker < ActiveRecord::Base
      has_many :appointments
    end
    

    在您的控制器中,您还需要获取@client。你目前是怎么做的?是current_user吗?如果不是,您需要在表单中传递客户 ID:

    <%= form_tag appointments_path do %>
        <%= select_tag(:worker_id, options_from_collection_for_select(@client.workers, :id, :alias), :include_blank =>true)%>
        <%= hidden_field_tag :client_id, session[:client_id] # or wherever you store this
        <%=submit_tag "Display"%>
    <% end %>
    

    现在您可以在控制器中找到客户端:

    @client = Client.find(params[:client_id])
    

    另请注意,在您提供的示例表单中,您将工作人员的 id 传递为:worker_id,因此您需要在控制器中使用params[:worker_id]。您提供的 JS 示例使用 :id 所以我假设这是您要使用的方法,因为您在控制器示例中使用了 params[:id] 。

    最后,将约会作为 JSON 数组返回:

    class AppointmentsController < ApplicationController
      def index
        if params[:id]
          @appointments = @client.workers.find(params[:id])
          render json: @appointments.map(&:as_json)
        else
          @appointments = "hello"
          p "*******hello id is not here*******"
        end  
    

    【讨论】:

    • 我用这个..它说参数丢失..我不知道为什么它会去appointmentscontroller#create
    • 它去那里是因为约会路径被传递给 form_tag。我不确定为什么它说参数丢失,你使用的是 Rails4 强参数吗?哪一行抛出了这个错误?
    【解决方案2】:

    我通过re-rendering在同一js file日历中再次找到了解决方案

    function RenderCalendar(eId) {
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        defaultView: 'agendaWeek',
        aspectRatio: 1.5,   
        slotMinutes: 30,
        slotEventOverlap: true,
        editable: true,
        timeFormat: 'h:mm t{ - h:mm t} ',
        titleFormat: {
            month: 'MMMM yyyy',                             // September 2009
            week: "| MMM d[ yyyy] { '&#8212;' MMM d | yyyy}", // |Sep 7 - Sep 13|, 2009
            day: 'dddd, MMM d, yyyy'                  // Tuesday, Sep 8, 2009
        },
    
     events: {
            url: '/appointments',
            cache: true,
            type: 'GET',
            data: {
                id: eId
            },
            error: function () {
                alert('there was an error while fetching events!');
            },
        },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多