私のイベントモデルには、フルカレンダーの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が正しくネストされているようにするにはどうすればよいですか?