Rails通过直通关系显示结果

时间:2018-11-14 17:43:16

标签: ruby-on-rails

现在,我正在显示一个TrainingEvents索引页面,其中包含每个位置的所有事件。我想做的就是为该特定区域创建一个TrainingEvents索引页面。

我的模型如下:

class TrainingEvents
 belongs_to :location, optional: true

class Locations
 belongs_to :region, optional: true
 belongs_to :user, optional: true
 has_many :careers, dependent: :destroy
 has_many :training_events, dependent: :destroy

class Regions
 has_many :locations, dependent: :destroy
 has_many :zipcodes, dependent: :destroy
 has_many :careers, through: :locations
 has_many :training_events, through: :locations
 has_and_belongs_to_many :logos
 has_and_belongs_to_many :employees

我的TrainingEventsController看起来像:

class TrainingEventsController < ApplicationController
  respond_to :json, :html
  before_action :filter_time

  def index
    @events = TrainingEvent.by_month(@time.month).ascending
    @events = @events.by_state(params[:state]) if params[:state].present?
    @events = @events.by_city(params[:city]) if params[:city].present?
  end

  def show
    @event = TrainingEvent.find_by!(url_name: params[:id])
    add_breadcrumb 'Home', root_path
    add_breadcrumb 'Training & Events', training_events_path
    add_breadcrumb @event.name
  end

  def region_index
    @events = TrainingEvent.by_month(@time.month).ascending
  end

  private

  def filter_time
    @time = if params[:month].present?
              params[:month].to_datetime
            else
              Time.zone.now
            end
  end
end

这里的想法是创建一个单独的方法region_index,然后沿新路线传递。因此,在我所在地区的显示页面上,我有以下内容:

<%= link_to 'View Region', region_index_path(@region) %>

链接转到正确的页面,但是加载数据是我遇到的问题。这是Slim视图:

= content_for :body do
  .container.banner-container
    .row.mt-4
      .col-sm-12
        = link_to 'Training Policies', '/training_policies', class: 'custom-button float-right'
    = form_tag :training_events, method: :get, id: 'filter-form' do
      .row.event-filter.align-items-center
        .col-lg-7.col-md-5
          = hidden_field_tag :month, params[:month]
          .month-selector
            = fa_icon 'chevron-left', data: { month: @time.last_month }
            h3 #{@time.strftime('%B %Y')}
            = fa_icon 'chevron-right', data: { month: @time.next_month }
        .col-lg-5.col-md-3.filter-select
          =link_to 'View All Trainings', training_events_path, class: 'custom-button float-right'
    - (@time.beginning_of_month.to_date..@time.end_of_month.to_date).each do |date|
      - events = @events.select { |event| event.time_start.to_date == date }
      - if events.count.positive?
        .row
          .col-sm-12
            .event-header
              span.date #{date.strftime('%d')}
              span #{date.strftime('%A')}
        - events.each do |event|
          .row.event-row
            .col-sm-8
              label.event-title #{event.name}
              .event-col-details
                - if event.time_start.present? && event.time_end.present?
                  = fa_icon 'clock-o'
                  p #{event.time_start.strftime('%l:%M %p')} - #{event.time_end.strftime('%l:%M %p')}
                - if event.street_address.present?
                  = fa_icon 'map-marker'
                  p #{event.street_address}
                - if event.city.present? && event.state.present?
                  = fa_icon 'map-marker'
                  p #{event.city}, #{event.state}
            .col-sm-4.details-cont
              = link_to 'view details', training_event_path(event.url_name)
          .row
            .col-sm-12
              .border

我知道我需要点击@ events.select并可能要点击位置然后再点击区域,但是我的脑袋撞墙了。我尝试了各种方法来做@ events.locations.select,这会得到NoM​​ethodError,甚至是@ events.regions.select,也将得到同样的结果。

那么我该如何建立直通关系并仅显示那些结果?

编辑: 我还尝试过在我的TrainingEvents中添加一个范围,以为我可以通过以下方式查询出与region.location_id匹配的位置ID:

scope :region_index, -> {where(location_id: region.location_id)}

我最终得到

  

未定义的局部变量或方法“区域”用于

     

是你的意思吗?关系

1 个答案:

答案 0 :(得分:0)

区域未在您的范围上下文中定义。我看到您已经熟悉使用变量的作用域,看起来您只是忘记了这些细节,需要新鲜的眼光。

scope :region_index, -> (region) { where(location_id: region.location_id)}