我的事件模型使用此方法覆盖fullcalender的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 / path是嵌套资源(region_city_event)。如何获取区域和城市值并将它们放在:url中,以便网址正确并嵌套?
答案 0 :(得分:0)
好的,因为索引操作是您使用事件的json响应的地方,您需要在用于将用户链接到索引html页面的javascript代码中使用相同的链接帮助程序。
在您看来,您可以这样:
<script>
var calendar_url = '<%= regions_cities_events_path(@region, @city) %>'
</script>
您需要在事件控制器中的索引操作中添加以下行:
@city = City.find params[:city_id]
答案 1 :(得分:0)
如果我理解你,你想把'/ region / city / event_id'映射到你的事件控制器show动作 - 为此,将这条路线添加到routes.rb:
match '/:region_id/:city_id/:event_id' => 'events#show'
然后在EventsController中的show方法中,您可以使用params[:region_id]
和params[:city_id]
来查找用户正在查找的区域和城市。