尝试添加删除按钮以删除用户

时间:2014-07-22 15:45:51

标签: ruby-on-rails-4 devise controller destroy

我正在跟随Michael Hartls教程,我正在尝试添加destroy动作以删除用户作为管理员。但是,该链接未显示在用户名下。我也在使用Devise,不确定这是否重要。任何帮助是极大的赞赏。感谢

控制器/用户

def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted."
    redirect_to users_path
  end


  def admin_user
      redirect_to(root_url) unless current_user.admin?
  end

_users.html

<li>
  <class= "round-image-50"><%= image_tag(current_user.avatar.url(:thumb)) %>
  <%= link_to user.name, user %>
  <% if current_user.admin? && !current_user?(user) %>
  <%= link_to "delete", user, method: :delete, data: { confirm: "You sure?" } %>
  <% end %>
</li>

路线

devise_for :admins
  devise_for :users

  resources :posts
  resources :users

  get "users/show"


  get 'feed', to: 'posts#index', as: :feed
  get "about" => "pages#about"
  root "pages#home"

  match 'users/:id' => 'users#show', via: :get

移植

class AddAdminToUsers < ActiveRecord::Migration
  def self.up
    add_column :users, :admin, :boolean, :default => false
  end

  def self.down
    remove_column :users, :admin
  end
end      

1 个答案:

答案 0 :(得分:0)

我能够弄清楚。以下代码需要转到我的用户索引页面而不是_user.html页面。感谢您的帮助@Pavan

<% if current_user.admin? && !current_user?(user) %>
  <%= link_to "delete", user, method: :delete, data: { confirm: "You sure?" } %>
<% end %>