Ruby on rails RAILS:如何使用[@祖父母,@Parent]符号为孙子模型生成链接(祖父母显示为Nil)

Ruby on rails RAILS:如何使用[@祖父母,@Parent]符号为孙子模型生成链接(祖父母显示为Nil),ruby-on-rails,nested,subtree,Ruby On Rails,Nested,Subtree,当我尝试显示、编辑、删除或添加地区时,遇到以下错误: undefined method `state_path' for #<#<Class:0x007f93a9e9df88>:0x007f93af11f8d8> 如果我进入国家/州/路径,一切正常。但是我宁愿使用模型符号来输入它,因为这在我的状态模型中是有效的 (我是否使用了正确的术语?如果没有,请更正,我还是Rails新手) 代码 区域模型 classdistrict“name” 私有的 def检查学校 开始 如果0

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

undefined method `state_path' for #<#<Class:0x007f93a9e9df88>:0x007f93af11f8d8>
如果我进入国家/州/路径,一切正常。但是我宁愿使用模型符号来输入它,因为这在我的状态模型中是有效的

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

代码 区域模型
classdistrict“name”
私有的
def检查学校
开始
如果0.count>0
self.errors[:base]“name ASC')
结束
#GET/districts/1
#GET/districts/1.json
def秀
结束
#获取/地区/新
def新
@地区=@state.districts.new
结束
#获取/地区/1/编辑
定义编辑
@state.districts.find(参数[:id])
结束
#邮政署/地区
#POST/districts.json
def创建
@地区=@state.districts.new(地区参数)
回应待办事项|格式|
如果@district.save
format.html{将_重定向到[@country,@state,@district],注意:'地区已成功创建。}
format.json{呈现操作:“显示”,状态::已创建,位置:@district}
其他的
format.html{呈现操作:'new'}
format.json{render json:@district.errors,status::unprocessable_entity}
结束
结束
结束
#补丁/放置/区域/1
#PATCH/PUT/districts/1.json
def更新
回应待办事项|格式|
如果@district.update(district_参数)
format.html{redirect_to[@state,@district],注意:“district已成功更新。”
format.json{head:no_content}
其他的
format.html{呈现操作:“编辑”}
format.json{render json:@district.errors,status::unprocessable_entity}
结束
结束
结束
#删除/地区/1
#删除/districts/1.json
def销毁
@district=@state.district.find(参数[:id])
回应待办事项|格式|
如果@district.destroy
format.html{将_重定向到@state}
format.json{head:no_content}
其他的
format.html{redirect_to(@state,:notice=>“无法删除具有地区的州”。)}
format.json{render json:@district.errors,status::unprocessable_entity}
结束
结束
结束
私有的
#使用回调在操作之间共享公共设置或约束。
德夫赛特尤区
@district=district.find(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def地区参数
参数require(:district).permit(:name,:state_id)
结束
def加载状态
@state=state.find(参数[:state\u id])
结束
结束
地区“表演”视图

名称:

状态:

|

不起作用的一行是链接到'Back',[@country,@state]你需要在show/edit/destroy方法中手动设置@country,因为@country为零

不过要注意。一般来说,它只能嵌套两层。所以:

Country
  State

State
  District

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

所以我需要像这样组织我的路线:

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

我希望输入信息的人能够很容易地判断出该设备与该学校A有关联。甚至可以很容易地将该设备连接回学区,以防它被移动到其他学校。

是的,这是正确的。因为你不需要人们访问/country/1/state这样的URL/2/districts/3/schools/3/networks/4/devices/5。您仍然可以使用面包屑或其他UI元素在界面中显示链接信息。除非您使用友好的ID,否则没有人会通过ID真正了解学校。至少,这是我的建议。
Country
  State

State
  District
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