RAILS: How to generate links for Grandchild Model using [@Grandparent, @Parent] notation (Grandparent shows as Nil)

StackOverflow https://stackoverflow.com/questions/17398004

  •  02-06-2022
  •  | 
  •  

Question

I'm running across the following error when I try to show, edit, delete, or add a district:

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

I'm expecting for it to generate a country_state_path link since I have link_to 'Back', [@country, @state] but for some reason it's only giving me state_path. If I use dot-notation instead doing @country.state I receive an nilClass error.

I'm using a Tree Structure for my models:

Country
    State
        District

If I enter country_state_path everything works fine. But I'd rather enter it using the model notation since that works in my State Model.

(Am I using the right terms? Please correct if not, I'm still new to Rails)

CODE

District Model

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

District Controller

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

District "Show" View

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

The line that isn't working is the "link_to 'Back', [@country, @state].

Était-ce utile?

La solution

You'll need to manually set @country in your show/edit/destroy methods, as @country is nil.

A note, though. Generally, it's best practice to only nest two deep. So:

Country
  State

State
  District

I know. I know. Don't shoot the messenger. Just passing the info along.

Autres conseils

So I'd need to structure my routes like so:

NetworkManager::Application.routes.draw do

    root to: "countries#index"

    resources :countries do
        resources
    end

    resources :states do
        resources :districts
    end

end

Instead of:

NetworkManager::Application.routes.draw do

    root to: "countries#index"

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

end

So, here's my end goal, maybe you have a better way to do this...

We setup schools across the country and each school has a variety of different network devices. The real purpose of this is to track what the network information is for each school, but I want to be able to organize it into Countries -> States -> Districts -> Schools -> Networks -> Devices.

If I want to do that I'm guessing it's better to do

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top