我希望能够使designer2只添加/创建电池。我不确定我错过了什么。管理员将能够管理所有,设计师将能够管理电池但无法访问角色和用户。 Designer2只会创建新的但无法删除或编辑,并且无法访问用户/角色。
ability.rb中的代码
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
elsif user.has_role? :designer1
can :manage, :Battery
elsif user.has_role? :designer2
can [:read,:add], Battery
end
else
can :read, :all
end
end
查看/电池/ index.html.slim
- model_class = Battery
form.navbar-search.pull-right
form.input-append method="get"
div.input-append
input.form-control placeholder="Search" type="text" name="keyword" value="#{params[:keyword]}"
- if params[:keyword]
= link_to batteries_path, class: 'btn' do
i.icon-remove
button.btn type="submit"
i.icon-search
div class="page-header"
h1=t '.title', :default => model_class.model_name.human.pluralize.titleize
table class="table table-striped"
thead
tr
th= model_class.human_attribute_name(:name)
th= model_class.human_attribute_name(:battery_type)
th= model_class.human_attribute_name(:manufacturer)
th= model_class.human_attribute_name(:model)
th= model_class.human_attribute_name(:volt)
th= model_class.human_attribute_name(:capacity)
th= model_class.human_attribute_name(:comments)
/ th= model_class.human_attribute_name(:created_at)
- if can? :manage, Battery
th=t '.actions', :default => t("helpers.actions")
tbody
- @batteries.each do |battery|
tr
td= battery.name
td= battery.battery_type
td= battery.manufacturer
td= battery.model
td= battery.volt.volt
td= battery.capacity.capacity
td= battery.comments
/ td=l battery.created_at
- if can? :manage, Battery
td
= link_to t('.edit', :default => t("helpers.links.edit")), edit_battery_path(battery), :class => 'btn btn-mini'
'
= link_to t('.destroy', :default => t("helpers.links.destroy")), battery_path(battery), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger'
- if can? :manage, Battery
= link_to t('.new', :default => t("helpers.links.new")), new_battery_path, :class => 'btn btn-primary'
= paginate @batteries, :theme => 'twitter-bootstrap'
br