所以目前我有一个嵌套项目。它从地板=>开关=>插孔开始。通过尝试创建一个新的插孔,我完成了所有工作。当我在开关节目中时,有一个新插孔和一个创建按钮的开放字段。当我输入插孔号码并点击创建时,就会出现错误。假设所有其他代码都是正确的(如果您想查看任何其他代码,请告诉我),我的千斤顶控制器中的 create 方法有什么问题?
应用程序/控制器/jacks_controller.rb:
...
def create
@floor = Floor.find(params[:floor_id])
@switch = @floor.switches.find(params[:switch_id])
@jack = @switch.jacks.create(params[:jack])
redirect_to(@switch)
end
...
它给我的错误是“没有路线匹配 [POST]”/floors/1/switches/1“”。这对我来说也毫无意义,因为这是我要添加插孔的当前交换机的确切 URL。
提前感谢任何可以解释我做错了什么或我的误解的人!
编辑:
这是我的耙子路线:
floor_switch_jacks GET /floors/:floor_id/switches/:switch_id/jacks(.:format) jacks#index
POST /floors/:floor_id/switches/:switch_id/jacks(.:format) jacks#create
new_floor_switch_jack GET /floors/:floor_id/switches/:switch_id/jacks/new(.:format) jacks#new
edit_floor_switch_jack GET /floors/:floor_id/switches/:switch_id/jacks/:id/edit(.:format) jacks#edit
floor_switch_jack GET /floors/:floor_id/switches/:switch_id/jacks/:id(.:format) jacks#show
PUT /floors/:floor_id/switches/:switch_id/jacks/:id(.:format) jacks#update
DELETE /floors/:floor_id/switches/:switch_id/jacks/:id(.:format) jacks#destroy
floor_switches GET /floors/:floor_id/switches(.:format) switches#index
POST /floors/:floor_id/switches(.:format) switches#create
new_floor_switch GET /floors/:floor_id/switches/new(.:format) switches#new
edit_floor_switch GET /floors/:floor_id/switches/:id/edit(.:format) switches#edit
floor_switch GET /floors/:floor_id/switches/:id(.:format) switches#show
PUT /floors/:floor_id/switches/:id(.:format) switches#update
DELETE /floors/:floor_id/switches/:id(.:format) switches#destroy
floors GET /floors(.:format) floors#index
POST /floors(.:format) floors#create
new_floor GET /floors/new(.:format) floors#new
edit_floor GET /floors/:id/edit(.:format) floors#edit
floor GET /floors/:id(.:format) floors#show
PUT /floors/:id(.:format) floors#update
DELETE /floors/:id(.:format) floors#destroy
home_index GET /home/index(.:format) home#index
root / home#index
这对 routes.rb 有好处吗?否则我的 routes.rb 是非常基础的,根本没有接触太多。如果那是我的问题所在,您能否帮助或发送给我一个不掩盖 routes.rb 部分的教程?
编辑:
我修好了它。问题出在我的 jacks/_form.html.erb 中的 line 表格中。正确的语法是:
<%= form_for [@floor, @switch, @switch.jacks.new] do |f| %>
感谢任何试图提供帮助的人,不胜感激!