我是关于铁路和尊重的新手,所以我希望在这里找到答案,
我这里有一些模特,而且所有模特都有很多关系
user - role_user - role - role_feature - feature
role_user和role_feature是连接多对多关系的中间表,我已经完成了所有控制器crud和模型关系表,并且还使用rspec对它进行了测试,它是100%覆盖,所以不要担心它。而我对视图,模型,控制器和rspect都没有做任何事情。
现在我想为每个用户创建访问现有功能的权限/权限,因此每个用户都有一个角色,而且该角色有很多功能,
文件seeds.rb 中的
RoleUser.where(role_id: 1, user_id:1).first_or_create;
Role.where(holding_company_id: 1, name: 'Area Index').first_or_create;
RoleFeature.where(role_id: 1, feature_id:1).first_or_create;
Feature.where(name: 'Area Index', key: 'area_index').first_or_create;
Feature.where(name: 'Area Create', key: 'area_create').first_or_create;
文件area.rb中的(我以示例区域为单位)
class AreasController < ApplicationController
before_filter :check_access
def index
if params[:per_page].blank? && params[:page].blank?
areas = Area.where(holding_company_id: current_holding_company.id)
else
areas = Area.where(holding_company_id: current_holding_company.id).paginate(page: params[:page], per_page: params[:per_page])
end
respond_to do |format|
format.json { render json: areas.to_json, status: 200 }
end
end
def create
end
def update
end
def show
end
def destroy
end
protected
def check_access
case params[:action]
when 'index'
unless current_user.roles.features.include? (area_index)
render nothing: true, status: 200
end
end
end
end
并且规范是
require 'spec_helper'
describe AreasController, :type => :controller do
before(:each) do
@token = FactoryGirl.create(:token)
@area = FactoryGirl.create(:area_1)
FactoryGirl.create(:area_2)
FactoryGirl.create(:area_3)
end
describe "GET 'index'" do
it "returns http success for json without pagination" do
get 'index', token: @token.token_string, format: :json
expect(response.status).to eq(200)
expect(JSON.parse(response.body).count).to eq(3)
expect(Area.count).to eq(3)
end
it "returns http success for json with pagination" do
get 'index', token: @token.token_string, format: :json, page: 1, per_page: 2
expect(response.status).to eq(200)
expect(JSON.parse(response.body).count).to eq(2)
end
end
describe "POST 'create'" do
end
describe "PUT 'update'" do
end
describe "GET 'show'" do
end
describe "DELETE 'destroy'" do
end
end
并且问题是在使用rspec测试它时出现错误/失败,它表示未定义的方法角色,我尝试修复它,我花了几个小时只是为了在互联网上浏览它,但它没用。所以我希望这对我来说是最后的机会,我需要你的帮助......