0

我的事件模型有这种方法来覆盖全日历的 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 正确且嵌套?

4

2 回答 2

0

如果我理解你,你想将'/region/city/event_id'映射到你的事件控制器显示动作——为此添加这个路由到routes.rb:

match '/:region_id/:city_id/:event_id' => 'events#show'

然后在 EventsController 中的 show 方法中,您可以使用params[:region_id]params[:city_id]找到用户正在寻找的地区和城市。

于 2012-08-27T17:54:43.327 回答
0

好的,由于 index 操作是您使用事件 json 响应的地方,因此您需要在用于将用户链接到 index html 页面的 javascript 代码中使用相同的链接帮助器。

在你看来,你可以有这样的东西:

<script>
  var calendar_url = '<%= regions_cities_events_path(@region, @city) %>'
</script>

您需要将以下行添加到事件控制器中的索引操作:

@city = City.find params[:city_id]
于 2012-08-27T17:53:29.027 回答