开发者

Rails 3 edit_path method not working

开发者 https://www.devze.com 2023-02-17 23:42 出处:网络
sorry if this is a dumb Q, this is my first Rails3 project... For some reason, this <%= link_to \'edit\', edit_geofence_path(geofence) %>

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 geofenc开发者_StackOverflowe 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!


<%= 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.


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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消