开发者

ROR Sub - Sub resource forms troubles

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/:hospit开发者_运维问答al_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"}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜