RAILS:如何使用[@Grandparent,@ Parent]表示法为Grandchild模型生成链接(祖父母显示为Nil)

时间:2013-07-01 05:35:37

标签: ruby-on-rails nested subtree

当我尝试显示,编辑,删除或添加区域时,我遇到以下错误:

undefined method `state_path' for #<#<Class:0x007f93a9e9df88>:0x007f93af11f8d8>

我期待它生成country_state_path链接,因为我有link_to 'Back', [@country, @state]但由于某种原因它只给了我state_path。如果我使用点符号代替@ country.state,我会收到一个nilClass错误。

我正在为我的模型使用树形结构:

Country
    State
        District

如果我输入country_state_path,一切正常。但我宁愿使用模型表示法输入它,因为它适用于我的状态模型。

(我是否使用了正确的条款?如果没有,请更正,我还是Rails的新手)

CODE

区域模型

class District < ActiveRecord::Base
    validates_uniqueness_of :name, scope: :state_id
    before_destroy :check_for_schools

    belongs_to :state
    #has_many :schools, :order => 'name'

    private
    def check_for_schools
=begin
        if schools.count > 0
            self.errors[:base] << "Cannot delete district while schools exist."
            return false
        end
=end
    end
end

区域管制员

class DistrictsController < ApplicationController
  # Allows JSON Queries
  skip_before_filter :verify_authenticity_token
  before_action :set_district, only: [:show, :edit, :update, :destroy]
  before_filter :load_state

  # GET /districts
  # GET /districts.json
  def index
    @districts = @state.districts.all(:order => 'name ASC')
  end

  # GET /districts/1
  # GET /districts/1.json
  def show
  end

  # GET /districts/new
  def new
    @district = @state.districts.new
  end

  # GET /districts/1/edit
  def edit
    @state.districts.find(params[:id])
  end

  # POST /districts
  # POST /districts.json
  def create
    @district = @state.districts.new(district_params)

    respond_to do |format|
      if @district.save
        format.html { redirect_to [@country,@state,@district], notice: 'District was successfully created.' }
        format.json { render action: 'show', status: :created, location: @district }
      else
        format.html { render action: 'new' }
        format.json { render json: @district.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /districts/1
  # PATCH/PUT /districts/1.json
  def update
    respond_to do |format|
      if @district.update(district_params)
        format.html { redirect_to [@state, @district], notice: 'District was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @district.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /districts/1
  # DELETE /districts/1.json
  def destroy
    @district = @state.district.find(params[:id])
    respond_to do |format|
      if @district.destroy
        format.html { redirect_to @state }
        format.json { head :no_content }
      else
          format.html { redirect_to( @state, :notice => 'Unable to delete a state that has districts.') }
          format.json { render json: @district.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_district
      @district = District.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def district_params
      params.require(:district).permit(:name, :state_id)
    end

    def load_state
      @state = State.find(params[:state_id])
    end
end

区“显示”视图

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @district.name %>
</p>

<p>
  <strong>State:</strong>
  <%= @district.state_id %>
</p>

<%= link_to 'Edit', edit_country_state_district_path(@country, @state, @district) %> |
<%= link_to 'Back', [@country, @state] %>

无法使用的行是"link_to 'Back', [@country, @state]

2 个答案:

答案 0 :(得分:0)

您需要在show / edit / destroy方法中手动设置@country,因为@country为零。

但是,请注意。一般来说,best practice只能嵌套两个深。所以:

Country
  State

State
  District

我知道。我知道。不要射击信使。只是传递信息。

答案 1 :(得分:0)

所以我需要像这样构建我的路线:

NetworkManager::Application.routes.draw do

    root to: "countries#index"

    resources :countries do
        resources
    end

    resources :states do
        resources :districts
    end

end

而不是:

NetworkManager::Application.routes.draw do

    root to: "countries#index"

    resources :countries do
        resources :states do
            resources :districts
        end
    end

end

所以,这是我的最终目标,也许你有更好的方法来做到这一点......

我们在全国各地设立学校,每所学校都有各种不同的网络设备。这样做的真正目的是跟踪每所学校的网络信息,但我希望能够将其组织起来 国家 - &gt;国家 - &gt;地区 - &gt;学校 - &gt;网络 - &gt;设备

如果我想这样做,我猜它最好做

Countries
    States

States
    Districts

Districts
    Schools

Schools
    Networks

Networks
    Devices

我希望输入信息的人能够轻松地告知该设备与该学校A相关联。如果必须将设备移到不同的学校,甚至可以将设备轻松连接到学区。