Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails creating a new doubly nested object
    primarykey
    data
    text
    <p>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?</p> <p>apps/controllers/jacks_controller.rb:</p> <pre><code> ... 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 ... </code></pre> <p>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.</p> <p>Thanks in advance to anyone who can explain what I'm doing wrong or my misunderstanding!</p> <p>EDIT:</p> <p>Here is my rake routes:</p> <pre><code> 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 </code></pre> <p>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?</p> <p>EDIT:</p> <p>I fixed it. The problem was in the form for line in my jacks/_form.html.erb. The correct syntax was:</p> <pre><code>&lt;%= form_for [@floor, @switch, @switch.jacks.new] do |f| %&gt; </code></pre> <p>Thanks to anyone who tried to help, it was appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload