简体   繁体   English

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

[英]Ruby on Rails creating a new doubly nested object

So currently I have a nested project. 所以目前我有一个嵌套项目。 It starts with floors=>switches=>jacks. 它以Floors => switchs => jacks开头。 I have everything working up through trying to create a new jack. 通过尝试创建新的插孔,我完成了所有工作。 When I am in the switch show there is an open field for a new jack and a create button. 当我进入切换秀时,会有一个用于新插孔和创建按钮的开放字段。 When I type in the jack number and hit create is when the error arises. 当我输入插孔编号并点击创建时,就会出现错误。 Assuming all other code is correct (let me know if you want to see any other code), what is wrong with my create method in my jacks controller? 假设所有其他代码正确(让我知道是否要查看其他代码),那么jacks控制器中的create方法有什么问题?

apps/controllers/jacks_controller.rb: apps / controllers / 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
  ...

The error it gives me is "No route matches [POST] "/floors/1/switches/1" ". 它给我的错误是“没有路由匹配[POST]“ / floors / 1 / switches / 1”“。 This also makes no sense to me because that is the exact URL for the current switch I'm adding jacks to. 这对我来说也没有意义,因为那是我要添加插孔的当前开关的确切URL。

Thanks in advance to anyone who can explain what I'm doing wrong or my misunderstanding! 在此先感谢任何可以解释我在做错或误解的人!

EDIT: 编辑:

Here is my rake routes: 这是我的耙路:

    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

Is this good for the routes.rb? 这对routes.rb有好处吗? Otherwise my routes.rb is very basic, haven't touched it much at all. 否则我的routes.rb是非常基础的,根本没有涉及。 If that's where my problem is, could you help or send me to a tutorial that doesn't gloss over the routes.rb part? 如果那是我的问题所在,您能帮忙还是把我带到一个没有覆盖routes.rb部分的教程?

EDIT: 编辑:

I fixed it. 我修好了它。 The problem was in the form for line in my jacks/_form.html.erb. 问题出在我的jacks / _form.html.erb中的表格行中。 The correct syntax was: 正确的语法是:

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

Thanks to anyone who tried to help, it was appreciated! 感谢任何尝试提供帮助的人,不胜感激!

I was running into a similar situation and solved it by changing my redirect_to statement. 我遇到了类似的情况,并通过更改redirect_to语句解决了该问题。 Try something like this: 尝试这样的事情:

redirect_to [@floor, @switch]

At the time I remember that the error message did not help me out, it was in fact throwing me off the trail. 当时我还记得错误消息并没有帮助我,实际上是使我偏离了轨道。

All your routes start with /floor , but I believe 您所有的路线都以/floor开头,但我相信

redirect_to @switch

Will try to resolve to a /switch path, which you don't have defined. 将尝试解析为您尚未定义的/switch路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails-嵌套关联-创建新记录 - Ruby on Rails - nested associations - creating new records 使用嵌套资源#rails创建一个新对象 - creating a new object with nested resources #rails ruby on rails,创建新对象,使用create或new方法? - ruby on rails, creating new object, use create or new method? 红宝石路轨。 未定义的方法错误,创建嵌套的资源对象 - Ruby rails. Undefined method error, creating nested resource object Ruby on Rails编辑/更新方法正在创建一个新对象,而不是更新 - Ruby on Rails edit/update method is creating a new object instead of updating 在验证创建新对象时获取“ undefined method” ruby​​? - Getting `undefined method` ruby on rails on validation creating new object? 在Ruby on Rails中创建新的模型对象时遇到麻烦-参数是符号吗? - Trouble creating a new model object in Ruby on rails - params are a symbol? Ruby on Rails新应用未创建 - Ruby on rails new app is not creating 从Ruby on Rails中的现有嵌套对象/模态创建嵌套对象/模态 - Creating a nested object/modal from an existing nested object/modal in Ruby on Rails 不创建类对象 - Ruby on Rails - Not creating class object - Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM