【问题标题】:Rails and devise user authorisationRails 并设计用户授权
【发布时间】:2015-08-19 06:16:45
【问题描述】:

我正在尝试在我的 Rails 应用程序中实现一些超级用户功能。我已经使用 Stripe 设置了几个免费计划。例如,我基本上希望 plan_id 1 上的用户能够查看带有上传按钮、编辑、删除帖子按钮等的完整页面,而 plan_id 2 上的用户(将是大多数用户)只能是能够查看帖子和上传,但无法看到上面列出的按钮。

任何人都可以安排要实现这一目标需要发生的事情吗?我在挣扎!

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 devise stripe-payments


    【解决方案1】:

    也许:

    # controller
    def action_show
      @user = User.current_user
    end
    
    def action_admin_only
      @user = User.current_user
      if @user.admin?
        # complete the action
      end
    end
    
    private
    #Must use strong params
    
    # helper
    def current_user
        if (user_id = session[:user_id])
            @current_user ||= User.find_by(id: user_id)
        end
    end
    
    # View
    <% if user.admin? %>
      <%= link_to "Delete", model_path, method: :delete, class: "button" %>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多