关于json:Fullcalendar Rails 3.2 – PG ::错误

Fullcalendar Rails 3.2 - PG::Error

我正在尝试使用rails 3.2为我的应用程序提供fullcalendar,并为dbms使用postgresql。
到了这一点,

在模型event.rb上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

for starts_at和ends_at我正在使用日期类型(不是日期时间)

在events.js.coffee上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$(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上

1
2
3
4
5
6
7
8
9
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 /我得到以下错误,如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
      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')):

更新

改变条件

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

并且在命令的帮助下

1
2
3
 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

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

1
2
3
4
5
6
7
8
9
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)

注意:资源事件

这是我的应用程序

1
2
3
4
5
6
7
8
9
10
11
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上

1
2
3
4
5
6
7
8
9
10
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)

谁能帮我?
谢谢 :)


你不能不喜欢:

1
a < starts_at < b

在WHERE子句中,这只是无效的SQL。 但是,您可以:

1
starts_at > a AND starts_at < b

你可能想要:

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

顺便说一句,带参数的作用域的推荐方法是使用类方法:

Using a class method is the preferred way to accept arguments for scopes.

所以你可能想要:

1
2
3
4
5
6
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