简体   繁体   中英

Ruby on Rails creating a new doubly nested object

So currently I have a nested project. It starts with floors=>switches=>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?

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" ". This also makes no sense to me because that is the exact URL for the current switch I'm adding jacks to.

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? Otherwise my routes.rb is very basic, haven't touched it much at all. 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?

EDIT:

I fixed it. The problem was in the form for line in my 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. 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

redirect_to @switch

Will try to resolve to a /switch path, which you don't have defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM