1
我試圖設置訪問嵌套資源(專業)前。 「... /公司/:ID /特產」。訪問不屬於我的公司工作正常。但我無法訪問我的專業。請幫助我,因爲我花了4個小時搜索解決方案而沒有任何結果。 我有以下幾點:Cancan錯誤,同時授權嵌套資源
慘慘1.6.9
//routes.rb
resources :companies do
resources :specialties
end
//ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.super_admin?
can :open, :admin_pages
else
cannot :open, :admin_pages
end
can [:edit, :update, :destroy], Company do |company|
company.try(:admin) == user
end
can :manage, Specialty
end
end
//companies_controller.rb
class CompaniesController < ApplicationController
load_and_authorize_resource
def new
@company = current_user.build_company
end
def create
@company = current_user.build_company params[:company]
if @company.save
redirect_to root_path, notice: I18n.t('notices.company_successfully_created')
else
render :new
end
end
def edit
@company = Company.find params[:id]
end
def update
@company = current_user.company
if @company.update_attributes(params[:company])
redirect_to root_path, notice: I18n.t('notices.company_successfully_updated')
else
render action: 'edit'
end
end
end
//specialties_controller.rb
class SpecialtiesController < ApplicationController
load_and_authorize_resource :company
load_and_authorize_resource through: :company
before_filter :company, except: [:destroy]
def index
@specialties = @company.specialties
respond_to do |format|
format.json {
resource = params[:resource_type]=='user' ? User.new : Profile.new
render :json => {:success => true, :html => (render_to_string '_specialties_list.html.slim', :locals => {:resource => resource})}
}
format.html {}
end
end
def new
@specialty = @company.specialties.build
end
def create
@specialty = @company.specialties.build params[:specialty]
if @specialty.save
redirect_to company_specialties_path, notice: I18n.t('notices.specialty_successfully_created')
else
render :new
end
end
def show
@specialty = Specialty.find params[:id]
end
def edit
@specialty = Specialty.find params[:id]
end
def update
@specialty = Specialty.find params[:id]
if @specialty.update_attributes(params[:specialty])
redirect_to company_specialties_path, notice: I18n.t('notices.specialty_successfully_updated')
else
render action: 'edit'
end
end
def destroy
@specialty = Specialty.find(params[:id])
@specialty.destroy
redirect_to company_specialties_path
end
private
def company
@company = Company.find(params[:company_id])
end
end
是否有任何特定操作會引發錯誤?什麼是堆棧跟蹤。如果您打算這樣做,請使用pastebin粘貼堆棧跟蹤並回復URL。 – manoj 2013-04-11 07:18:25
例如(公司id = 3是我的)http:// localhost:3000/companies/3/specialities不能訪問,而我可以訪問http:// localhost:3000/companies/3/edit和I不能訪問(和我所期望的一樣)http:// localhost:3000/companies/2/edit,所以我不能訪問嵌入在公司資源中的任何內容 – 2013-04-11 08:35:44
我收到「您無權訪問此頁面。」 – 2013-04-11 08:42:27