1

sorry if this is a dumb Q, this is my first Rails3 project...

For some reason, this <%= link_to 'edit', edit_geofence_path(geofence) %>

renders as <a href="/geofence/edit.2">edit</a> (my geofence's id is 2).

And <%= link_to 'delete', {:action=>'destroy', :id=>geofence}, :confirm=>"You sure?", :method=> :delete %>

renders as <a href="/geofence?id=2" data-confirm="You sure?" data-method="delete" rel="nofollow">delete</a>,

which might be fine, but clicking the link generates this in the logs Started GET "/geofence?id=2". So, not DELETE, just GET.

My routes.rb file is just resource :geofence.

On a related note, for some reason the default action for a geofence is "show". So /geofence/ DOES NOT call the index method, it calls the show method. I think that also must be wrong.

I'm done cursing at this app for now, I'm going to take a day to cool off and hopefully get this SIMPLE SCAFFOLD working tomorrow night... Help me, stackoverflow! You're my only hope!

ColinK
  • 438
  • 1
  • 3
  • 8

3 Answers3

2
<%= link_to 'delete', {:action=>'destroy', :id=>geofence}, :confirm=>"You sure?", :method=> :delete %>

should be:

<%= link_to 'delete', {:action=>'destroy', :id=>geofence}, :confirm=>"You sure?", :method=> :delete, :remote => true %>

Without :remote => true, the click isn't handled by javascript.

And in your routes.rb file, you should have that defined as:

resources :geofence

Setting it as resource implies that there is only one, and is causing a lot of your weird behavior.

ctide
  • 5,217
  • 1
  • 29
  • 26
  • just wanted to add that with if you run rake routes in your terminal you should be able to see whats going on. resource :geofence will create a route that looks like /:controller/:action/.:format - no id parameter is expected, hence when you try to pass one (in this case 2) it gets interpreted as a format thing. resources :geofence will give you /:controller/:action/:id – Will Ayd Mar 19 '11 at 01:51
  • oh my god, the internet's hivemind solved my problem in less time than it took me to fail in the first place. stack overflow is incredible! Thanks so much! – ColinK Mar 19 '11 at 03:41
0

As a side note, to complete ctide answer I would suggest you to use the plural form of your controllers name as a convention. It will sound more natural to put:

resources :geofences

inside your routes.rb file.

Here is a previous StackOverflow question, about using the plural form as a convention for controllers.

Community
  • 1
  • 1
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
0

When you use resource :geofence in your routes file you are telling your application that there is only one geofence resource, and that it is not a collection. You will get show, update, create, new, but not index - and the id value will not be used because there is only one resource. (The show action here will have the path /geofence

If you use resources :geofences (notice the pluralization) then you've defined a collection of resources, /geofences will now give you the index action and your url helpers will work correctly with the show action rendering /geofences/3.

Hope this helps you understand why the plural form is necessary for this sort of resource :)

nzifnab
  • 15,876
  • 3
  • 50
  • 65