当我尝试编辑通过脚手架创建的RoR对象时导致此错误的原因是什么?

时间:2010-08-29 08:13:38

标签: ruby-on-rails

当我点击笔记的编辑按钮时导致此错误的原因是什么?删除按钮工作正常。我用脚手架创建了注释对象。

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

错误

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 配置/ routes.rb中

map.resources :notes

所有其他笔记路线都可以正常工作。

感谢您阅读

1 个答案:

答案 0 :(得分:2)

脚手架控制器的“编辑”操作,以及defult路由支持“编辑”为GET而不是POST请求。

如果您使用的是link_to而不是button_to,那么事情应该会有效。

或者,更改包含button_to -

的行
<%= button_to 'Edit', edit_note_path(note), :method => :get %>