我的事件模型有这种方法来覆盖全日历的 json 视图。
def as_json(options = {})
{
:id => self.id,
:title => self.title,
:slug => self.slug,
: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)
}
end
我的人际关系是:
class Event
belongs_to: city
end
class City
belongs_to: region
has_many: events
end
class Region
has_many: cities
end
控制器
def index
@region = Region.find(1)
@cities = @region.cities
# full_calendar will hit the index method with query parameters
# 'start' and 'end' in order to filter the results for the
# appropriate month/week/day. It should be possiblt to change
# this to be starts_at and ends_at to match rails conventions.
# I'll eventually do that to make the demo a little cleaner.
@events = Event.scoped
@events = @events.after(params['start']) if (params['start'])
@events = @events.before(params['end']) if (params['end'])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @events }
format.js { render :json => @events }
end
end
# GET /events/1
# GET /events/1.xml
def show
@event = Event.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @event }
format.js { render :json => @event.to_json }
end
end
正确的 url/路径是嵌套资源 (region_city_event)。如何获取区域和城市值并将它们放在 :url 中,以便 url 正确且嵌套?