ROR子 - Sub资源形成麻烦

时间:2011-01-11 23:57:06

标签: ruby-on-rails resources modeling

感谢大家以前的帮助。我将假设这将是一个相当棘手的问题但是我有一个问题,我似乎无法弄明白。

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

Hospital
   |--> Firewall
      |--> fwzones

除了新表格之外,我几乎已经弄明白了。

这是我的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控制器

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

我认为这与医院或防火墙模型有关,但我真的无法弄明白。任何帮助表示赞赏。哦,这是我的rake路线,而我们在它。

    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"}

1 个答案:

答案 0 :(得分:1)

这里你在许多防火墙上调用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-with-rails