rails_admin如何使列可排序且可查询

时间:2016-04-17 16:52:11

标签: ruby-on-rails ruby rails-admin

我已经把这个问题搞得好几天了,并尝试了我能想到的一切。尝试使用 rails_admin 修改其他人创建的应用程序。我想要做的就是在列表视图中创建一个字段Course,以按字母顺序排序和查询。我有一个模型home_study_registration

  • HomeStudyRegistration继承自ClassRegistration
  • class_registration has_one:course,:through => :course_class
  • :course_class是belongs_to:course
  • 的抽象模型
  • :course有一个字符串属性:title,现在会显示在表格中,点击后会导航到该课程的 show 视图。

后者是我想要修改的。有没有办法让这个列可以排序?另外,如何添加过滤器以按:title属性进行搜索?这是我的home_study_registration.rb模型:

class HomeStudyRegistration < ClassRegistration
  default_scope { joins(:course_class, :course, :user).where('course_classes.class_type = ?', "home").readonly(false) }

  delegate :full_name, :email, :profession, :ce_state, :ce_number, :to => :user, :prefix => true
  delegate :eacc, :to => :course_class
  delegate :title, :to => :course

  RailsAdmin.config do |config|
    config.model HomeStudyRegistration do 
      navigation_label 'Course Management'
      parent HomeStudy
      label 'Manage Home Study'

      list do
        sort_by :created_at
        filters [:created_at, :status]

        field :user do
          label 'name'
          column_width 120
        end

        field :cart do
          label 'order'
          column_width 50
        end
        field :user_ce_state do
          label 'Lic. St.'
          column_width 25
          pretty_value do
            if bindings[:object].licenses.any?
              bindings[:object].licenses.order(:created_at).collect{ |l| l.state }.join("<br>").html_safe
            else
              bindings[:object].user_ce_state
            end
          end
        end
        field :user_profession do
          label 'Lic. Type'
          column_width 100
          pretty_value do
            if bindings[:object].licenses.any?
              bindings[:object].licenses.order(:created_at).collect{ |l| l.show_profession }.join("<br>").html_safe
            else
              bindings[:object].user_profession
            end
          end
        end
        field :user_ce_number do
          label 'Lic. '
          column_width 80
          pretty_value do
            if bindings[:object].licenses.any?
              bindings[:object].licenses.order(:created_at).collect{ |l| l.number }.join("<br>").html_safe
            else
              bindings[:object].user_ce_number
            end 
          end
        end
        field :eacc do
          column_width 60
        end
        field :course do
          label 'Course' 
          column_width 200
        end
        field :score do
          column_width 55
        end
        field :status, :enum do
          column_width 55
          enum do
            ['Active', 'Completed', 'Expired', 'Errors']
          end
        end
        field :exam_completed_at do
          label 'Completed'
        end
        field :created_at do
          label 'registered'
        end

      end

      edit do
        field :course_class do
          associated_collection_scope do
                # bindings[:object] & bindings[:controller] are available, but not in scope's block!
                Proc.new { |scope|
                  scope = scope.where(class_type: "home")
                }
            end
        end
        field :user
        field :status, :enum do
          enum do
            ['Active', 'Completed', 'Expired', 'Errors']
          end
        end
        field :exam_completed_at do
          label 'Completed'
        end
        field :exam_attempts
        field :score
      end

      export do
        field :user_full_name
        field :user_email
        field :cart_id do
          label 'Order'
        end
        field :user_ce_state do
          label 'Lic. state'
        end
        field :user_profession do
          label 'Lic. type'
        end
        field :user_ce_number do
          label 'Lic. #'
        end
        field :eacc
        field :course_title
        field :score
        field :status
        field :exam_completed_at do
          label 'Completed'
        end
        field :created_at do
          label 'Registered'
        end
      end    
    end
  end
end

如有必要,我可以缩写。但我认为显示其他方法和字段的上下文可能会有所帮助。我一直关注的领域是:

...

list do
        sort_by :created_at
        filters [:created_at, :status]

...

        field :course do
          label 'Course' 
          column_width 200
        end

...

我尝试添加sortable: truesortable :title以及可过滤标记和“pretty_value”绑定的所有变体。这个问题严重缩短了我的生活。有人可以帮忙吗?

由于

1 个答案:

答案 0 :(得分:0)

field :course do
      label 'Course' 
      column_width 200
      sortable "courses.name" #courses.lastname or courses.firstname
  end