ROR Sub - 子资源形式麻烦

发布于 2024-10-11 16:38:32 字数 5172 浏览 6 评论 0原文

感谢大家之前的帮助。我假设这将是一个非常n00bish 的问题,但这是我遇到的问题,而且我似乎无法弄清楚。

基本上, 我正在跟踪几家医院的防火墙及其区域/接口。所以我的模型看起来

Hospital
   |--> Firewall
      |--> fwzones

除了新形式之外我几乎已经弄清楚了所有事情。

这是我的routes.rbhospital.rbfirewall.rbfwzone.rbfwzonecontoller

mine::Application.routes.draw do

  resources :hospitals do
    resources :firewalls do
        resources :fwzones
    end
  end
end

class Hospital < ActiveRecord::Base
    has_many :firewalls, :dependent => :destroy
end

尝试

class Firewall < ActiveRecord::Base
  belongs_to :hospital
  has_many :fwzones
end

认为它与医院或

class Fwzone < ActiveRecord::Base
  belongs_to :firewall
end

class FwzonesController < ApplicationController
....
  def new
    @hospital = Hospital.find(params[:hospital_id])
    @firewall = @hospital.firewalls.find(params[:firewall_id])
    @fwzone = @firewall.fwzones.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @fwzone }
    end
  end
....
end

过的表单字符串

<%= form_for([@hospital,@hospital.firewalls.fwzones.build]) do |f| %>

Error: undefined method `fwzones' for #<Class:0x1bba030>
-------------------
<%= form_for([@firewall,@firewalls.fwzones.build]) do |f| %>
<%= form_for([@hospital,@firewalls.fwzones.build]) do |f| %>

Error: undefined method `fwzones' for nil:NilClass
-------------------
<%= form_for([@hospital.firewalls,@fwzones]) do |f| %>

Error: undefined method `model_name' for NilClass:Class

防火墙模型有关,但我真的无法弄清楚。任何帮助表示赞赏。哦,这是我的耙子路线。

    hospital_firewall_fwzones GET    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones(.:format)          {:action=>"index", :controller=>"fwzones"}
    hospital_firewall_fwzones POST   /hospitals/:hospital_id/firewalls/:firewall_id/fwzones(.:format)          {:action=>"create", :controller=>"fwzones"}
 new_hospital_firewall_fwzone GET    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/new(.:format)      {:action=>"new", :controller=>"fwzones"}
edit_hospital_firewall_fwzone GET    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/:id/edit(.:format) {:action=>"edit", :controller=>"fwzones"}
     hospital_firewall_fwzone GET    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/:id(.:format)      {:action=>"show", :controller=>"fwzones"}
     hospital_firewall_fwzone PUT    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/:id(.:format)      {:action=>"update", :controller=>"fwzones"}
     hospital_firewall_fwzone DELETE /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/:id(.:format)      {:action=>"destroy", :controller=>"fwzones"}
           hospital_firewalls GET    /hospitals/:hospital_id/firewalls(.:format)                               {:action=>"index", :controller=>"firewalls"}
           hospital_firewalls POST   /hospitals/:hospital_id/firewalls(.:format)                               {:action=>"create", :controller=>"firewalls"}
        new_hospital_firewall GET    /hospitals/:hospital_id/firewalls/new(.:format)                           {:action=>"new", :controller=>"firewalls"}
       edit_hospital_firewall GET    /hospitals/:hospital_id/firewalls/:id/edit(.:format)                      {:action=>"edit", :controller=>"firewalls"}
            hospital_firewall GET    /hospitals/:hospital_id/firewalls/:id(.:format)                           {:action=>"show", :controller=>"firewalls"}
            hospital_firewall PUT    /hospitals/:hospital_id/firewalls/:id(.:format)                           {:action=>"update", :controller=>"firewalls"}
            hospital_firewall DELETE /hospitals/:hospital_id/firewalls/:id(.:format)                           {:action=>"destroy", :controller=>"firewalls"}
                    hospitals GET    /hospitals(.:format)                                                      {:action=>"index", :controller=>"hospitals"}
                    hospitals POST   /hospitals(.:format)                                                      {:action=>"create", :controller=>"hospitals"}
                 new_hospital GET    /hospitals/new(.:format)                                                  {:action=>"new", :controller=>"hospitals"}
                edit_hospital GET    /hospitals/:id/edit(.:format)                                             {:action=>"edit", :controller=>"hospitals"}
                     hospital GET    /hospitals/:id(.:format)                                                  {:action=>"show", :controller=>"hospitals"}
                     hospital PUT    /hospitals/:id(.:format)                                                  {:action=>"update", :controller=>"hospitals"}
                     hospital DELETE /hospitals/:id(.:format)                                                  {:action=>"destroy", :controller=>"hospitals"}

Thanks to everyone for their previous help. I am going to assume that this is going to be a pretty n00bish question but is a problem that I have and I can't seem to figure it out.

Basically,
I am tracking firewalls and their zones/interfaces for a couple of hospitals. so my model looks like

Hospital
   |--> Firewall
      |--> fwzones

I have gotten almost everything figured out except the new form.

here is my routes.rb

mine::Application.routes.draw do

  resources :hospitals do
    resources :firewalls do
        resources :fwzones
    end
  end
end

hospital.rb

class Hospital < ActiveRecord::Base
    has_many :firewalls, :dependent => :destroy
end

firewall.rb

class Firewall < ActiveRecord::Base
  belongs_to :hospital
  has_many :fwzones
end

fwzone.rb

class Fwzone < ActiveRecord::Base
  belongs_to :firewall
end

fwzone contoller

class FwzonesController < ApplicationController
....
  def new
    @hospital = Hospital.find(params[:hospital_id])
    @firewall = @hospital.firewalls.find(params[:firewall_id])
    @fwzone = @firewall.fwzones.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @fwzone }
    end
  end
....
end

the form strings that I have tried

<%= form_for([@hospital,@hospital.firewalls.fwzones.build]) do |f| %>

Error: undefined method `fwzones' for #<Class:0x1bba030>
-------------------
<%= form_for([@firewall,@firewalls.fwzones.build]) do |f| %>
<%= form_for([@hospital,@firewalls.fwzones.build]) do |f| %>

Error: undefined method `fwzones' for nil:NilClass
-------------------
<%= form_for([@hospital.firewalls,@fwzones]) do |f| %>

Error: undefined method `model_name' for NilClass:Class

I assume that it has something to do with the hospital or firewall model but I really can't figure it out. Any help is appreciated. Oh, here is my rake route while we are at it.

    hospital_firewall_fwzones GET    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones(.:format)          {:action=>"index", :controller=>"fwzones"}
    hospital_firewall_fwzones POST   /hospitals/:hospital_id/firewalls/:firewall_id/fwzones(.:format)          {:action=>"create", :controller=>"fwzones"}
 new_hospital_firewall_fwzone GET    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/new(.:format)      {:action=>"new", :controller=>"fwzones"}
edit_hospital_firewall_fwzone GET    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/:id/edit(.:format) {:action=>"edit", :controller=>"fwzones"}
     hospital_firewall_fwzone GET    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/:id(.:format)      {:action=>"show", :controller=>"fwzones"}
     hospital_firewall_fwzone PUT    /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/:id(.:format)      {:action=>"update", :controller=>"fwzones"}
     hospital_firewall_fwzone DELETE /hospitals/:hospital_id/firewalls/:firewall_id/fwzones/:id(.:format)      {:action=>"destroy", :controller=>"fwzones"}
           hospital_firewalls GET    /hospitals/:hospital_id/firewalls(.:format)                               {:action=>"index", :controller=>"firewalls"}
           hospital_firewalls POST   /hospitals/:hospital_id/firewalls(.:format)                               {:action=>"create", :controller=>"firewalls"}
        new_hospital_firewall GET    /hospitals/:hospital_id/firewalls/new(.:format)                           {:action=>"new", :controller=>"firewalls"}
       edit_hospital_firewall GET    /hospitals/:hospital_id/firewalls/:id/edit(.:format)                      {:action=>"edit", :controller=>"firewalls"}
            hospital_firewall GET    /hospitals/:hospital_id/firewalls/:id(.:format)                           {:action=>"show", :controller=>"firewalls"}
            hospital_firewall PUT    /hospitals/:hospital_id/firewalls/:id(.:format)                           {:action=>"update", :controller=>"firewalls"}
            hospital_firewall DELETE /hospitals/:hospital_id/firewalls/:id(.:format)                           {:action=>"destroy", :controller=>"firewalls"}
                    hospitals GET    /hospitals(.:format)                                                      {:action=>"index", :controller=>"hospitals"}
                    hospitals POST   /hospitals(.:format)                                                      {:action=>"create", :controller=>"hospitals"}
                 new_hospital GET    /hospitals/new(.:format)                                                  {:action=>"new", :controller=>"hospitals"}
                edit_hospital GET    /hospitals/:id/edit(.:format)                                             {:action=>"edit", :controller=>"hospitals"}
                     hospital GET    /hospitals/:id(.:format)                                                  {:action=>"show", :controller=>"hospitals"}
                     hospital PUT    /hospitals/:id(.:format)                                                  {:action=>"update", :controller=>"hospitals"}
                     hospital DELETE /hospitals/:id(.:format)                                                  {:action=>"destroy", :controller=>"hospitals"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浸婚纱 2024-10-18 16:38:32

在这里,您在许多防火墙上调用 fwzones

<%= form_for([@hospital,@hospital.firewalls.fwzones.build]) do |f| %>

,并且应该是

<%= form_for([@hospital, @firewall, @firewall.fwzones.build]) do |f| %>

其他的只是复数和不存在变量的随机猜测。

你似乎确实在猜测你的方法,我建议你买一本书并学习你的方法。从长远来看,您将节省大量时间。

http://pragprog.com/titles/rails4/agile-web-development -带导轨

Here you're calling fwzones on many firewalls

<%= form_for([@hospital,@hospital.firewalls.fwzones.build]) do |f| %>

and should be

<%= form_for([@hospital, @firewall, @firewall.fwzones.build]) do |f| %>

The others are just random guesses of plurals and non-existant variables.

You do seem to be guessing your way along and I recommend you buy a book and learn your way along. You'll save a lot of time in the long run.

http://pragprog.com/titles/rails4/agile-web-development-with-rails

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文