0

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!

3
  • Can you post the relevant part of your routes.rb file? If you can browse to /floors/1/switches/1 but can't post there, that sounds like a routing issue. Commented Jun 15, 2012 at 17:56
  • Well, I don't see any route that looks like POST /floors/:floor_id/switches/:id, which is what the error is saying doesn't exist. Commented Jun 15, 2012 at 18:08
  • That makes sense. I'm trying to use jacks like I did switches, but it doesn't work the same way with my current route configuration. Is there any way I can either add that route or use an existing route to add a new jack? Commented Jun 15, 2012 at 18:21

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Did this work? If this answer solved your problem, it would help others who might run into something similar if you marked it as accepted. Thx.
Hey sorry, I've been gone since Thursday and haven't had much time to work on it. No, it did not work, I'm still having the same problem. I tried the new redirect_to and I'm still getting: No route matches [POST] "/floors/1/switches/1"
I got it to work! Believe me, I did search beforehand, but now I did fine a similar situation to mine and I fixed it. I'll edit the main post for info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.