【问题标题】:Show or hide fields depending on the Acl9 role - Ruby on Rails根据 Acl9 角色显示或隐藏字段 - Ruby on Rails
【发布时间】:2011-02-25 23:49:53
【问题描述】:

我正在使用 Acl9 来管理角色,如果用户具有角色:customer,我想隐藏复选框用户类型,如果角色是:manager,则显示它。我希望只有 :manager 可以编辑所有字段以及 :customer 的一些字段。

感谢您的帮助!

<h1>Editing user</h1>

<% form_for(@user) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :usertype %><br />
    <%= f.check_box :usertype %>
  </p>
  <p>
    <%= f.label :surname %><br />
    <%= f.text_field :surname %>
  </p>
  <p>
    <%= f.label :firstname %><br />
    <%= f.text_field :firstname %>
  </p>
  <p>
    <%= f.label :phone %><br />
    <%= f.text_field :phone %>
  </p>
  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </p>
  <p>
    <%= f.label :registrationdate %><br />
    <%= f.datetime_select :registrationdate %>
  </p>
  <p>
    <%= f.label :login %><br />
    <%= f.text_field :login %>
  </p>
  <p>
    <%= f.label :password %><br />
    <%= f.text_field :password %>
  </p>
  <p>
    <%= f.submit 'Update' %>
  </p>
<% end %>

<%= link_to 'Show', @user %>
<%= link_to 'Back', users_path %>

【问题讨论】:

    标签: ruby-on-rails acl9


    【解决方案1】:

    根据Acl9 documentation,你应该可以这样做:

    <% if @user.has_role?(:manager, nil) %> 
      <p>
        <%= f.label :usertype %><br />
        <%= f.check_box :usertype %>
      </p>
    <% end %>
    

    【讨论】:

      【解决方案2】:

      你也可以这样想:

      ..在你的 application_helper.rb 中

      module ApplicationHelper
        include Acl9Helpers
      end
      

      ..在你看来,类似的事情

      <% show_to(:admin) do %>
        The content to show
      <% end %>
      

      【讨论】:

        猜你喜欢
        • 2017-08-03
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        • 2012-12-14
        • 2017-12-13
        • 2018-07-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多