Rails 应用显示 Button/Link 取决于用户["rank"]

Rails App Display Button/Link Depending on Users["rank"]

好的,这个问题的目的是让我弄清楚如何根据存储在数据库中的用户属性在视图上显示组件。我是 rails 的新手,所以弄清楚如何处理数据将帮助我更好地掌握框架。

注意:我为用户身份验证使用 Devise,为我的数据库使用 SQlite3。

这是我试图找出的示例:我有一个 table "Users",其中包含每个用户的信息,其中包括一个元素 "rank"。根据用户排名(如果排名 >= 2),我想显示一个按钮供用户转到新视图。进入该视图后,我想再次验证用户排名(出于安全目的)。

如果您希望我 edit/add 回答我的问题以更好地帮助您确定解决方案,请告诉我。提前谢谢大家!



Schema.rb

ActiveRecord::Schema.define(version: 2020_01_04_235503) do

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.string "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "userid"
    t.string "companyid"
    t.string "properties"
    t.string "rank"
    t.string "firstname"
    t.string "lastname"
    t.string "username"
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["username"], name: "index_users_on_username", unique: true
  end

end



仪表板#index

<!DOCTYPE html>
<html>
  <head>
    <title>Berwald | Dashboard</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

  </head>

  <body>

    <nav class="navbar navbar-dark bg-dark">
      <div class="navbar-nav mr-auto">
        <img src="https://berwald.s3.us-east-2.amazonaws.com/images/berwald_logo.png" class="nav-logo">
      </div>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle nav-dropdown" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Account
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item nav-dropdown-button" href="">Settings</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item"><%= link_to 'Sign out', destroy_user_session_path, :method => :delete, class: "nav-dropdown-button" %></a>
        </div>
      </li>
    </nav>

    <div class="card" style="width: 18rem;">
      <img src="..." class="card-img-top" alt="...">
      <div class="card-body">
        <h5 class="card-title">Create New Property</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="#" class="btn btn-primary">Add</a>
      </div>
    </div>

    <div class="card" style="width: 18rem;">
      <img src="..." class="card-img-top" alt="...">
      <div class="card-body">
        <h5 class="card-title">Manage Users</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="#" class="btn btn-primary">Go</a>
      </div>
    </div>


    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
  </body>
</html>

假设您正在使用 ERB,用户启用或禁用 [​​=16=] 的代码将是...

<% if current_user.rank >= 2 %>
  <%= link_to customers_path %>
<% end %>

在 CustomersController 中你可以做

class CustomersController < ApplicationController

  before_action :check_user_rank, only: :index

  def index
    # show index of customers
  end

  private

  def check_user_rank
    if current_user.rank < 2
      flash[:error] = 'You are not allowed to do this action'
      redirect_back(fallback: root_path)
    end
  end

end