2015-03-03 81 views
-1

我有兩類用戶:「用戶」和「商店」。我想讓管理員成爲一個「用戶」,並能夠刪除商店,並且遇到麻煩。刪除作爲「用戶」對象的「商店」對象admin

我能得到的刪除按鈕在shops/index.html.erb露面,如果登錄的用戶是管理員(如下圖所示),但是當我嘗試刪除店鋪對象我得到的錯誤The action 'destroy' could not be found for ShopsController

店/ index.html.erb

<% provide(:title, 'All shops') %> 
<h1>All Shops</h1> 

<ul class="center hero-unit col-md-6 col-md-offset-3 shops"> 
    <% @shops.each do |shop| %> 
    <li> 
     <div class= "shop-name pull-left"> 
     <%= link_to shop.name, shop %> 
     <% if current_user.admin? && !current_shop?(shop) %> 
     | <%= link_to "(Delete Shop)", shop, method: :delete, 
             data: { confirm: "You sure?" } %> 
     <% end %> 
     </div> 
     <div class= "shop-address pull-right"> 
     <p><%= shop.address %> <br> <%= shop.city %>, <%= shop.state %> <%= shop.zip %> <br> <%= shop.phone %></p> 
     </div> 
    </li> 
    <% end %> 
</ul> 

的破壞行動是在商店控制器,但:

class ShopsController < ApplicationController 
    before_action :logged_in_shop, only: [:edit, :update] 
    before_action :logged_in_user, only: :destroy 
    before_action :correct_shop, only: [:edit, :update] 
    before_action :admin_user,  only: :destroy 

    def index 
     @shops = Shop.all 
    end 

    def show 
     @shop = Shop.find(params[:id]) 
    end 

    def new 
     @shop = Shop.new 
    end 

    def create 
     @shop = Shop.new(shop_params) 
     if @shop.save 
     shop_log_in @shop 
     flash[:success] = "Thank you for signing up, welcome to ensage!" 
     redirect_to shop_home_path 
     else 
     render 'new' 
     end 
    end 

    def edit 
     @shop = Shop.find(params[:id]) 
    end 

    def update 
     @shop = Shop.find(params[:id]) 
     if @shop.update_attributes(shop_params) 
     flash[:success] = "Profile updated" 
     redirect_to @shop 
     else 
      render 'edit' 
     end 
    end 

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

    private 

    def shop_params 
     params.require(:shop).permit(:name, :address, :city, :state, :zip, :email, :phone, :password, 
            :password_confirmation, :picture) 
    end 

    def correct_shop 
     @shop = Shop.find(params[:id]) 
     redirect_to(root_url) unless current_shop?(@shop) 
    end 

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

end 

我怎麼能允許管理員用戶刪除購物?

+1

可以請您分享店鋪控制器的完整代碼嗎? – Dabrorius 2015-03-03 09:30:28

+0

@Dabrorius完成。我嘗試將第4行更改爲'before_action:admin_user,只有:: destroy',這不起作用 – 2015-03-03 09:32:52

+0

那麼你現在有答案:)你的銷燬方法是私有的。 – Dabrorius 2015-03-03 09:33:54

回答

0

您已將您的銷燬方法設置爲私有,因此無法訪問。

before_action :logged_in_shop, only: [:edit, :update, :destroy] 

你可能想要刪除:從這裏破壞,否則會希望你的登錄身份店,以摧毀它。我不確定您的訪問控制是如何工作的,但請注意不要公開訪問。

+0

該行已包含在商店控制器行2中。我需要用戶銷燬商店,因爲只有一個用戶將成爲管理員。 – 2015-03-03 09:44:09

+0

您是否嘗試將此行更改爲此? before_action:logged_in_shop,只有:[:edit,:update] – Dabrorius 2015-03-03 09:47:34

+0

一併添加'before_action:logged_in_user,只有:: destroy'和'before_action:admin_user,只有:: destroy'。不知道我的任何更改是否會在其他方面造成問題,但現在仍在運行。 – 2015-03-03 09:52:31

0

您已將行動destroy放在private部分,這就是爲什麼它是不可見的。建議您升級過來private關鍵字

0

第一招destroy行動了私人,其次我看不到admin_shop方法在任何地方,無論是刪除此行或定義admin_shop方法

before_action :admin_shop, only: :destroy 

要添加admin_shop方法

def admin_shop 
    unless current_user.admin? 
    flash[:error] = "Not authorized" 
    redirect_to shops_url 
    end  
end 

在您的destroy行動

def destroy 
    user = User.find(current_user.id) 
    Shop.find(params[:id]).destroy 
    sign_in user, bypass: true 
    flash[:success] = "Shop deleted" 
    redirect_to shops_url 
end 
+0

該行是店鋪控制器中的第4行。但我需要admin_user才能銷燬商店 – 2015-03-03 09:35:34

+0

這很好,但我無法在任何地方看到'admin_shop'方法,您在哪裏定義了它? – RSB 2015-03-03 09:36:32

+0

不會有admin_shop,代碼需要進行編輯,以便只有用戶是管理員。 – 2015-03-03 09:37:55