管理员将用户注册为项目

时间:2019-12-10 15:14:27

标签: ruby ruby-on-rails-5

我已经花了几天时间尝试构建此功能。您能帮我一些指导吗?

目前,我的平台上有其他项目。每个用户都可以免费注册任何项目。但是现在我想改变这一点。只有管​​理员才能将用户注册到特定项目。而且用户只能看到管理员也已经注册了他的项目

这是项目负责人

class ProjectController < ApplicationController
  before_action :authenticate_user!

  def index
      @projects = Project.all.order(:tag)
  end

  def show
    @project = Project.find(params[:id])
    @tasks = @project.tasks.order(:tag)

    @joined = false

    if !current_user.nil? && !current_user.projects.nil?
      @joined = current_user.projects.include?(@project)
    end

    @users = @project.users.order('created_at desc').first(10)
  end

  def list
    if !current_user.nil?
      @projects = current_user.projects
    end
  end
end

这是我的项目模型

class Project < ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: [:slugged, :finders]

  has_many :tasks, dependent: :destroy

  has_many :subscriptions
  has_many :users, through: :subscriptions, dependent: :destroy

  validates :name, presence: true, length: { maximum: 50 }
  validates :content, presence: true

  has_one_attached :image

  def shortname
    name.length > 25? name[0..24] + "..." : name
  end
end

这是充电控制器

class ChargesController < ApplicationController
  before_action :authenticate_user!

  def free
    project = Project.find(params[:project_id])
    current_user.subscriptions.create(project: project)

    redirect_to project
  end
end

这是订阅模式

class Subscription < ApplicationRecord
  belongs_to :project
  belongs_to :user
end

2 个答案:

答案 0 :(得分:0)

目前,我可以使用收费功能通过rails控制台完成

user_name.subscriptions.create(project: project_name)

答案 1 :(得分:0)

我发现的另一个不错的选择是安装gem active_admin。从那里,我可以轻松导入Subscription模型并将用户注册为项目。

相关问题