Ruby on Rails创建一个新的双嵌套对象

时间:2012-06-15 17:54:23

标签: ruby-on-rails ruby-on-rails-3

所以目前我有一个嵌套项目。它以floors => switches => jacks开头。通过尝试创建一个新的插孔,我已经做好了一切准备。当我在开关节目中时,有一个用于新插孔和创建按钮的开放字段。当我输入插孔号码并且在出现错误时点击创建。假设所有其他代码都是正确的(如果你想看到任何其他代码,请告诉我),我的插孔控制器中的创建方法出了什么问题?

应用/控制器/ 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部分的教程吗?

编辑:

我修好了。问题是我的插孔/ _form.html.erb中的行的形式。正确的语法是:

<%= form_for [@floor, @switch, @switch.jacks.new]  do |f| %>

感谢任何试图提供帮助的人,我们对此表示赞赏!

1 个答案:

答案 0 :(得分:3)

我遇到了类似的情况并通过更改我的redirect_to声明解决了这个问题。尝试这样的事情:

redirect_to [@floor, @switch]

当时我记得错误信息并没有帮助我,实际上是让我离开了。

您的所有路线都以/floor开头,但我相信

redirect_to @switch

将尝试解析为您未定义的/switch路径。