解释删除和销毁有什么问题?

时间:2015-05-07 12:24:29

标签: ruby-on-rails

无法删除存储在数据库中的数据请帮忙吗?

这是我的sages_controller.rb

def delete
   @sage = Sage.find(params[:id])
end

  def destroy
    Sage.find(params[:id]).destroy
    redirect_to '/thanksss'
  end

这是我的delete.html.erb

<%= form_for @sage do |f| %>
    <%= f.label :Name %>
    <%= f.text_field :Name %>
    <br/>
    <%= f.label :Email %>
    <%= f.text_field :Email %>
    <br/>
    <%= f.label :Address %>
    <%= f.text_field :Address %>
    <br/>
    <%= f.label :Number %>
    <%= f.text_field :Number %>
    <br/>
    <%= f.submit 'Delete' %>
<% end %>

这是我的routes.rb

get 'sages/delete/:id'=> 'sages#delete'
delete 'sages/delete/:id'=> 'sages#destroy'

2 个答案:

答案 0 :(得分:1)

您无需为此创建表单。 button_to应该有效

<%= button_to "delete", @sage, :method => :delete, :class => :destroy %>

这将在内部生成form DELETE方法,与下面的默认GET方法相反,

<%= form_for @sage, :method => :delete do  %>   

答案 1 :(得分:0)

你可以这样做, 将您的按钮更改为链接,

<%= link_to "delete", @sage, :method=>:delete %>

将路线文件更改为

resources :sages,only: [:new,:create,:edit, :update, :delete]
get 'sages/delete/:id'=> 'sages#delete'

更改您的删除方法,例如

def delete
  @sage = Sage.find(params[:id])
  destroy
end