我有两类用户:“用户”和“商店”。我想让管理员成为一个“用户”,并能够删除商店,并且遇到麻烦。删除作为“用户”对象的“商店”对象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