2

I have the following in my routes.rb:

match "/profile/permissions" => 'profiles#edit_permissions', :as => 'edit_permissions', :via => :get    
match "/profile/permissions" => 'profiles#update_permissions', :as => 'update_permissions', :via => :post

Getting /profile/permissions works find, yet, when I post to /profile/permissions, I get:

Started POST "/profile/permissions" for 127.0.0.1 at 2011-03-29 12:12:09 -0400
ActionController::RoutingError (No route matches "/profile/permissions"):

Any ideas?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
  • 2
    What's the output of `rake routes`? – Andrew Marshall Mar 29 '11 at 16:20
  • edit_permissions GET /profile/permissions(.:format) {:controller=>"profiles", :action=>"edit_permissions"} update_permissions POST /profile/permissions(.:format) {:controller=>"profiles", :action=>"update_permissions"} – Larry Reaves Mar 29 '11 at 16:22
  • What does the url look like when you make the POST? You can check via Firebug, chrome's net tab, etc. It should be http://localhost/profile/permissions. If it's not that may be your problem. – McStretch Mar 29 '11 at 16:30
  • Yeah, those routes look fine. Does a restart of the rails server clear up the problem, if you are using the default dev one? – Kelly Mar 29 '11 at 16:34
  • http://localhost:3000/profile/permissions I'm using default development server ('rails server'), and restarting does not change things. – Larry Reaves Mar 29 '11 at 16:36
  • Do you have a `update_permissions` method defined in `profiles_controller`? – McStretch Mar 29 '11 at 16:45
  • Ok... Firebug to the rescue! Somehow I had a _method: put in my parameters. I'm still not sure where it is coming from, but overriding it before the request with _method: post solves my problem. Strange that the error still referred to it as "Started POST" – Larry Reaves Mar 29 '11 at 16:46
  • Oh very interesting. I wonder if it has to do with the fact that "update" typically maps as an action to the PUT verb. Can you run a little test and see what happens if you change it to `profiles#create_permissions` instead? `create` is typically used for POST. I wonder if Rails is just reading "update" and setting up a PUT. This is all hypothetical of course, I don't have the code in front of me right now :P – McStretch Mar 29 '11 at 16:50
  • 1
    The _method is being set as a form parameter, probably because I'm using form_for with a resource that isn't new. But, yeah, you're on the right track... Maybe I should be using PUT anyway. – Larry Reaves Mar 29 '11 at 16:53
  • 1
    PUT in rails is typically used for updating a record, so if you're updating then yeah go with PUT. If you're creating a record then go with POST. – McStretch Mar 29 '11 at 16:55

1 Answers1

1

Can you put your routes.rb in a gist? Sometimes there's a conflict, and extra eyes are needed to find it. Also, for the sake of fidgeting, you might try out the new shorthand:

get "/profile/permissions" => 'profiles#edit_permissions', :as => 'edit_permissions'    
post "/profile/permissions" => 'profiles#update_permissions', :as => 'update_permissions'

Unto themselves, both your routing text and this example successfully trigger their actions on the specified controller.

Start a test app, with only a routing file, run its server, and try to post to those URLs. If you want to use the Rails console, check out this.

> require "uri"
> require "net/http"
> url = app.update_permissions_url
> url = "http://localhost/profile/permissions" (if your domain is bollocks)
> Net::HTTP.post_form(URI.parse(url),{})

Easy to minimalize your environment for testing, now.

Community
  • 1
  • 1
nessur
  • 1,143
  • 1
  • 11
  • 18
  • I was actually originally using the get and post, the match was a change I made in my efforts to get it working. Useful information, but as I said above, the problem turned out to be a _method: put parameter. Since I am in fact updating a record a put is what I should be using anyway. The fact that the log says POST when for the purpose of routing it is a PUT was not very helpful, but c'est la vie. – Larry Reaves Mar 29 '11 at 17:11
  • Oh nice. Just late to the party :) – nessur Mar 29 '11 at 17:34