ROR Sub - 子资源形式麻烦
感谢大家之前的帮助。我假设这将是一个非常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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,您在许多防火墙上调用 fwzones
,并且应该是
其他的只是复数和不存在变量的随机猜测。
你似乎确实在猜测你的方法,我建议你买一本书并学习你的方法。从长远来看,您将节省大量时间。
http://pragprog.com/titles/rails4/agile-web-development -带导轨
Here you're calling fwzones on many firewalls
and should be
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