我的事件模型有這種方法來覆蓋fullcalender的json視圖。如何用引用對象覆蓋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就是正確的並且嵌套了?
您需要向我們顯示您的控制器和視圖代碼,以瞭解您想要實現的目標。簡而言之,帶日曆的頁面必須具有區域和城市的上下文才能通過。如果你只是想顯示所有事件,而不是關心該地區和城市,那麼創建另一條路線來支持它。 – agmcleod
剛剛更新問題與控制邏輯 – Remco