views:

34

answers:

1

What is causing this error when I click on the edit button for a note? The delete button works fine. I created the note object with a scaffold.

index.html.erb

<% @notes.each do |note| %>
    <%= note.detail %>
    <%= button_to 'Delete', note, :confirm => 'Are you sure?', :method => :delete %>
    <%= button_to 'Edit', edit_note_path(note) %>
<% end %>

notes_controller.rb

before_filter :check_ownership, :except => [:new, :create, :index, :edit]
def edit
    @note = Note.find(params[:id])
end

Error

ActiveRecord::RecordNotFound in NotesController#192 
Couldn't find Note with ID=edit
../app/controllers/notes_controller.rb:248:in `check_ownership'
Parameters:
{"id"=>"edit"}

EDIT config/routes.rb

map.resources :notes

All of the other routes for notes work fine.

Thanks for reading

+2  A: 

Scaffold controller's 'edit' action, and defult routing support 'edit' as GET rather than POST request.

If you use link_to rather than button_to, things should work.

Alternatively, change the line containing button_to -

<%= button_to 'Edit', edit_note_path(note), :method => :get %>
amba
@amba - Thank you so much, this works! If it's not too much trouble, could I ask why there's a difference between link_to and button_to?
ben
hey ben, link_to generates a simple hyperlink (<a href="...">...</a>), the default request generated by this is a GET. whereas button_to generates a whole form structure, with a 'submit' button within it. forms default action type is POST.I think the difference between the default request types for hyperlinks (GET) and form buttons (POST) is beyond rail's control - this is HTML.
amba
@amba - Ah, I get it know. Thanks for the help!
ben