我已经花了几天时间尝试构建此功能。您能帮我一些指导吗?
目前,我的平台上有其他项目。每个用户都可以免费注册任何项目。但是现在我想改变这一点。只有管理员才能将用户注册到特定项目。而且用户也只能看到管理员也已向他注册的项目
这是项目负责人
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
目前,我可以使用收费功能通过Rails控制台进行此操作
user_name.subscriptions.create(project: project_name)
我发现的另一个不错的选择是安装gem active_admin。从那里,我可以轻松导入订阅模型并将用户注册到项目。