Ruby on rails 在Active Admin Rails中创建新项目时筛选下拉列表
我有一个Rails 5应用程序,它通过标准的Ruby on rails 在Active Admin Rails中创建新项目时筛选下拉列表,ruby-on-rails,devise,activeadmin,rolify,Ruby On Rails,Devise,Activeadmin,Rolify,我有一个Rails 5应用程序,它通过标准的用户模型使用设计进行注册和会话。我还有Rolify集成了两种角色(学生、教师) 但是现在发生的事情是,当我想添加一个新的特色教师时,我会在下拉列表中得到数据库中用户的完整列表 我要做的是,在活动管理UI中将新用户添加到特色教师列表时,仅显示角色类型为“教师”的用户,以及尚未添加到特色教师表的用户 有人能帮我吗?我对Ruby和Rails有点陌生,希望了解如何完成这项工作。我很可能在其他活动管理界面中也需要它 谢谢。您需要为特色教师创建表单,请在特色教师.
用户
模型使用设计
进行注册和会话。我还有Rolify
集成了两种角色(学生、教师)
但是现在发生的事情是,当我想添加一个新的特色教师时,我会在下拉列表中得到数据库中用户的完整列表
我要做的是,在活动管理UI中将新用户添加到特色教师列表时,仅显示角色类型为“教师”的用户,以及尚未添加到特色教师表的用户
有人能帮我吗?我对Ruby和Rails有点陌生,希望了解如何完成这项工作。我很可能在其他活动管理界面中也需要它
谢谢。您需要为特色教师创建表单,请在
特色教师.rb
活动管理文件中添加以下代码
form do |f|
f.inputs 'Featured Teacher' do
# in the select box only load those users which are teacher and not in featured teacher list you need to add query for it
f.input :user, as: :select, collection: User.where(role: 'teacher').map { |u| [u.name, u.id] }, include_blank: true, allow_blank: false, input_html: { class: 'select2' }
# please also add other fields of featured teacher model
您可以查看active admin文档中的所有可用选项非常好!我一直想知道如何过滤进入控制器的数据。当然,根据我的说法,这是一种更好的方法。@Sharath您能不能也对这个答案进行更新投票,以便将来对其他用户有所帮助:)
class CreateFeaturedTeachers < ActiveRecord::Migration[5.2]
def change
create_table :featured_teachers, id: :uuid do |t|
t.references :user, foreign_key: true,type: :uuid
t.timestamps
end
end
end
class FeaturedTeacher < ApplicationRecord
belongs_to :user
end
ActiveAdmin.register FeaturedTeacher do
permit_params :user_id
actions :index,:new, :destroy
index do
selectable_column
column :user
column :created_at
actions name: "Actions"
end
end
form do |f|
f.inputs 'Featured Teacher' do
# in the select box only load those users which are teacher and not in featured teacher list you need to add query for it
f.input :user, as: :select, collection: User.where(role: 'teacher').map { |u| [u.name, u.id] }, include_blank: true, allow_blank: false, input_html: { class: 'select2' }
# please also add other fields of featured teacher model