Rails视图与关联中的关联

时间:2013-04-22 02:59:51

标签: ruby-on-rails simple-form

我使用简单的形式和体面的曝光 - 我有一个访问的观点 - 一个家庭进行访问,每个家庭有一个或多个邻居或家庭成员。我试过这个:

= simple_form_for visit do |f|
  = f.association :household, :label => 'Household',prompt:'choose a household'
  - household.neighbors.each do |neighbor|
    %tr
      %td= link_to neighbor.name, neighbor_path(neighbor)
      %td= neighbor.age
      %td= neighbor.visits.count

我可以选择一个家庭与协会,但我很难显示属于家庭的邻居 - 我上面的工作在家庭视图,但它在这里不起作用 - 我需要从家庭返回家庭关联使其可用于邻居查找

我该怎么做?

我的控制器:

class VisitsController < ApplicationController
  expose(:visit)
  expose(:visits)
  helper_method :visit, :visits

def create
  if visit.save
    redirect_to visits_path, notice: 'Visit was successfully created.'
  else
    render action: "new"
  end
end

def update
  if visit.save
    redirect_to visits_path, notice: 'Visit was successfully updated.'
  else
    render action: "edit"
  end
end

def destroy
  visit.destroy

  redirect_to visits_path
end

private

def allowable
  [
    :household_id, :neighbor_id, :visited_on, :items_received, :notes
  ]
end

  def visit_params
   params.require(:visit).permit(*allowable)
  end
end

0 个答案:

没有答案