0

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

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 的新手)

代码

地区模式

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].

4

2 回答 2

0

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

不过,请注意。一般来说,最好只嵌套两个深度。所以:

Country
  State

State
  District

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

于 2013-07-01T06:00:19.703 回答
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

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

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

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

Countries
    States

States
    Districts

Districts
    Schools

Schools
    Networks

Networks
    Devices

I want the person entering the information to be able to tell that this device is associated with this School A easily. And potentially even have the device be easily connected back to the district in case it has to be moved to a different school.

于 2013-07-01T06:14:22.230 回答