我有兩類用戶:「用戶」和「商店」。我想讓管理員成爲一個「用戶」,並能夠刪除商店,並且遇到麻煩。刪除作爲「用戶」對象的「商店」對象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
我怎麼能允許管理員用戶刪除購物?
可以請您分享店鋪控制器的完整代碼嗎? – Dabrorius 2015-03-03 09:30:28
@Dabrorius完成。我嘗試將第4行更改爲'before_action:admin_user,只有:: destroy',這不起作用 – 2015-03-03 09:32:52
那麼你現在有答案:)你的銷燬方法是私有的。 – Dabrorius 2015-03-03 09:33:54