【问题标题】:Fullcalendar Rails 3.2 - PG::ErrorFullcalendar Rails 3.2 - PG::Error
【发布时间】:2012-12-15 08:02:54
【问题描述】:

我正在尝试使用 rails 3.2 为我的应用程序使用 fullcalendar,并将 postgresql 用于 dbms。 言归正传,

关于模型事件.rb

class Event < ActiveRecord::Base

  attr_accessible :all_day, :ends_at, :description, :name, :starts_end

  scope :between, lambda {|start_time, end_time|
    {:conditions => ["? < starts_at < ?", Event.format_date(start_time), Event.format_date(end_time)] }
  }

  # need to override the json view to return what full_calendar is expecting.
  # http://arshaw.com/fullcalendar/docs/event_data/Event_Object/
  def as_json(options = {})
    {
      :id => self.id,
      :name => self.name,
      :description=> self.description || "",
      :start => starts_at.rfc822,
      :end => ends_at.rfc822,
      :allDay => self.all_day,
      :recurring => false,
      :url => Rails.application.routes.url_helpers.event_path(id),
      #:color => "red"
    }

  end

  def self.format_date(date_time)
    Time.at(date_time.to_i).to_formatted_s(:db)
  end

end

对于starts_at 和ends_at,我使用的是日期类型(不是日期时间)

关于 events.js.coffee

$(document).ready ->
  $('#calendar').fullCalendar
    editable: true,
    header:
      left: 'prev,next today',
      center: 'title',
      right: 'month,agendaWeek,agendaDay'
    defaultView: 'month',
    height: 500,
    slotMinutes: 30,

    eventSources: [{
      url: '/admin'+'/events',
    }],

    timeFormat: 'h:mm t{ - h:mm t} ',
    dragOpacity: "0.5"

    eventDrop: (event, dayDelta, minuteDelta, allDay, revertFunc) ->
      updateEvent(event);

    eventResize: (event, dayDelta, minuteDelta, revertFunc) ->
      updateEvent(event);


updateEvent = (the_event) ->
  $.update "/events/" + the_event.id,
    event:
      name: the_event.name,
      starts_at: "" + the_event.start,
      ends_at: "" + the_event.end,
      description: the_event.description

关于控制器 admin/events_controller.rb

def index
    @events = Event.scoped
    @events = Event.between(params['start'], params['end']) if (params['start'] && params['end'])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @events }
    end
  end

首先,我插入一个事件并成功,当运行到 localhost:3000/admin/events/ 时,我收到以下错误,例如

      Event Load (14.0ms)  SELECT "events".* FROM "events" WHERE ('2012-11-25 00:00:
    00' < starts_at < '2013-01-06 00:00:00')
    PG::Error: ERROR:  syntax error at or near "<"
    LINE 1: ...events"  WHERE ('2012-11-25 00:00:00' < starts_at < '2013-01...
                                                                 ^
    : SELECT "events".* FROM "events"  WHERE ('2012-11-25 00:00:00' < starts_at < '2
    013-01-06 00:00:00')
    Completed 500 Internal Server Error in 100ms

    ActiveRecord::StatementInvalid (PG::Error: ERROR:  syntax error at or near "<"
    LINE 1: ...events"  WHERE ('2012-11-25 00:00:00' < starts_at < '2013-01...
                                                                 ^
    : SELECT "events".* FROM "events"  WHERE ('2012-11-25 00:00:00' < starts_at < '2
    013-01-06 00:00:00')):

更新

改变条件

{:conditions => [
  "starts_at > ? and starts_at < ?",
  Event.format_date(start_time), Event.format_date(end_time)
] }

在命令提示符下

 Event Load (2.0ms)  SELECT "events".* FROM "events" WHERE (starts_at > '2012-1
-25 00:00:00' and starts_at < '2013-01-06 00:00:00')
ompleted 200 OK in 31ms (Views: 15.0ms | ActiveRecord: 12.0ms)

但事件未显示在日历中:(

更新 2

我正在尝试示例应用程序 和事件显示。

Started GET "/events?start=1353776400&end=1357405200&authenticity_token=rTzzn2j+
DpUwKRwjPAdSjfq17glTu8dFjrKo2dGbEo8=&_=1355566671618" for 127.0.0.1 at 2012-12-1
5 17:17:51 +0700
Processing by EventsController#index as JSON
  Parameters: {"start"=>"1353776400", "end"=>"1357405200", "authenticity_token"=
>"rTzzn2j DpUwKRwjPAdSjfq17glTu8dFjrKo2dGbEo8=", "_"=>"1355566671618"}
  ←[1m←[36mEvent Load (1.0ms)←[0m  ←[1mSELECT "events".* FROM "events" WHERE ('2
012-11-25 00:00:00' < starts_at < '2013-01-06 00:00:00')←[0m
Completed 200 OK in 34ms (Views: 31.0ms | ActiveRecord: 1.0ms)

注意:资源事件

这是我的应用程序

Started GET "/admin/events?start=1353776400&end=1357405200&_=135556713595
4" for 127.0.0.1 at 2012-12-15 17:25:36 +0700
Processing by Admin::EventsController#index as JSON
  Parameters: {"start"=>"1353776400", "end"=>"1357405200", "_"=>"1355567135954"}
 Admin Load (2.0ms)  SELECT "public"."admins".* FROM "public"."ad
mins" WHERE "public"."admins"."id" = 25 LIMIT 1
  Account Load (6.0ms)  SELECT "public"."accounts".* FROM "public"."accounts" WH
ERE "public"."accounts"."admin_id" = 25 LIMIT 1
  Event Load (3.0ms)  SELECT "events".* FROM "events" WHERE (starts_at > '2012-1
1-25 00:00:00' and starts_at < '2013-01-06 00:00:00')
Completed 200 OK in 101ms (Views: 72.0ms | ActiveRecord: 24.0ms)

注意:命名空间管理员 (admin/event) 下的资源事件,并且我有架构的多租户,管理员使用公共架构登录后登录更改到其他架构(子域)。

我将日期类型更改为日期时间

在 psql 上

education_development=# SELECT * FROM subdomain.events WHERE (starts_at > '2
012-11-25 00:00:00' and starts_at < '2013-01-06 00:00:00')
education_development-# ;
 id |     name      |       starts_at        |        ends_at         | all_day
|   description    |         created_at         |         updated_at
----+---------------+------------------------+------------------------+---------
+-----------------+----------------------------+----------------------------
  1 | New Year 2013 | 2013-01-01 00:00:00+07 | 2013-01-01 00:00:00+07 | f
| New Year 2013 | 2012-12-15 06:53:50.695456 | 2012-12-15 06:53:50.695456
(1 row)

谁能帮帮我? 谢谢:)

【问题讨论】:

    标签: ruby-on-rails json postgresql ruby-on-rails-3.2


    【解决方案1】:

    你只能这样:

    a < starts_at < b
    

    在 WHERE 子句中,这不是有效的 SQL。但是,您可以输入:

    starts_at > a AND starts_at < b
    

    你可能想要:

    {:conditions => [
      "starts_at > ? and starts_at < ?",
      Event.format_date(start_time), Event.format_date(end_time)
    ] }
    

    顺便说一句,带参数作用域的推荐方法是use a class method

    使用类方法是接受范围参数的首选方式。

    所以你可能想要:

    def self.between(start_time, end_time)
      where('starts_at > :lo and starts_at < :up',
        :lo => Event.format_date(start_time),
        :up => Event.format_date(end_time)
      )
    end
    

    【讨论】:

    • 对不起,这是我的错,显然命名:starts_end 应该是:starts_at
    • 但是错误信息告诉你 PostgreSQL 不喜欢 a &lt; b &lt; c 形式的东西。
    • 我的情况有所改变,但日历上没有显示活动。
    • when get starts_at > '2012-1 -25 00:00:00' and starts_at
    • 您可能应该删除一天中的时间部分以避免可能的时间一问题。你为什么希望你的约会出现?如果您在 psql 中尝试查询,或者在 Rails 控制台中检查查询输出,会发生什么情况?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多