我正在尝试设置对嵌套资源(专业)的访问权限。“.../companies/:id/specialties”。访问不属于我的公司工作正常。但我无法进入我的专长。请帮助我,因为我花了 4 个小时搜索解决方案,但没有任何结果。我有以下内容:
康康 1.6.9
//routes.rb
resources :companies do
resources :specialties
end
//能力.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
//company_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