使用Ruby on Rails创建删除确认页面3

时间:2011-01-13 19:05:22

标签: ruby-on-rails ruby-on-rails-3 routing action

我正在尝试向我的RoR3应用程序添加一个新页面,该应用程序应显示用户帐户的删除确认。它应匹配'ROOT_RAILS / controllers / accounts_controller.rb'中的'destroy'操作。

此时我的问题出现在创建“link_to”这个页面上,但也许我错了,我的工作尚未完成。

所以,我所做的是:

  1. 我创建了'ROOT_RAILS / views / accouns / delete.html.erb'文件。

  2. 我像这样更新了routes.rb:

    resources :accounts do
      collection do
        get 'delete'
        post 'delete'
      end
    end
    
  3. 我不知道接下来的步骤,但现在如果我尝试插入此代码

    <%= link_to 'Delete', delete_account_path(@current_account) %>
    

    在我的观点中,我会收到此错误:

    undefined method `delete_account_path' for #<#<Class:0x00...>
    

    我需要做什么?


    这个“link_to”有效,但当然不符合我的要求:

    <%= link_to 'Delete', delete_users_accounts_path %>
    

1 个答案:

答案 0 :(得分:4)

尝试以下方法:

配置/ routes.rb中:

resources :accounts do
  get :delete, :on => :member
end

在删除页面之前的视图中:

<%= link_to 'Delete', delete_account_path(@current_account) %>

在删除视图中(这将调用控制器中的destroy方法):

<%= link_to 'Delete', @current_account, :confirm => "Are you sure?", :method => :delete %>