0

管理 gem指南 表明资源标签可以适应该模型的属性

def display_resource(user)
  user.name
end

但是,这似乎不适用于关系

Card属于_to User ,目标是在 CardDashboard 中代表资源用户

card.user.last_name + ', ' + card.user.first_name

然而,即使是一个简洁的定义

def display_resource(user)
  card.user.last_name
end

导致undefined local variable or method 'card' for #<CardDashboard

看来 display_resource 定义只适用于它自己的类

def display_resource(user)
  user.last_name + ', ' + user.first_name
end

将工作。

是否有一种机制可以使用嵌套属性在每个类的基础上定义资源标签?

更新 错误堆栈

app/dashboards/user_dashboard.rb:212:in `display_resource'
app/views/admin/cards/_form.html.erb:38
app/views/admin/cards/_form.html.erb:36:in `each'
app/views/admin/cards/_form.html.erb:36
app/views/admin/cards/_form.html.erb:17
app/views/admin/cards/edit.html.erb:35 

更新

完整的堆栈跟踪

administrate (0.14.0) lib/administrate/base_dashboard.rb:39:in `block in attribute_type_for'
administrate (0.14.0) lib/administrate/base_dashboard.rb:38:in `fetch'
administrate (0.14.0) lib/administrate/base_dashboard.rb:38:in `attribute_type_for'
administrate (0.14.0) lib/administrate/page/base.rb:30:in `attribute_field'
administrate (0.14.0) lib/administrate/page/form.rb:15:in `block in attributes'
administrate (0.14.0) lib/administrate/page/form.rb:14:in `map'
administrate (0.14.0) lib/administrate/page/form.rb:14:in `attributes'
app/views/admin/cards/_form.html.erb:36
actionview (6.0.3.4) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (6.0.3.4) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (6.0.3.4) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (6.0.3.4) lib/action_view/helpers/form_helper.rb:452:in `form_for'
client_side_validations (17.2.0) lib/client_side_validations/action_view/form_helper.rb:11:in `form_for'
app/views/admin/cards/_form.html.erb:17
actionview (6.0.3.4) lib/action_view/base.rb:274:in `_run'
actionview (6.0.3.4) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.3.4) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.3.4) lib/action_view/template.rb:385:in `instrument_render_template'
actionview (6.0.3.4) lib/action_view/template.rb:183:in `render'
actionview (6.0.3.4) lib/action_view/renderer/partial_renderer.rb:357:in `block in render_partial'
actionview (6.0.3.4) lib/action_view/renderer/abstract_renderer.rb:88:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.3.4) lib/action_view/renderer/abstract_renderer.rb:87:in `instrument'
actionview (6.0.3.4) lib/action_view/renderer/partial_renderer.rb:346:in `render_partial'
actionview (6.0.3.4) lib/action_view/renderer/partial_renderer.rb:317:in `render'
actionview (6.0.3.4) lib/action_view/renderer/renderer.rb:65:in `render_partial_to_object'
actionview (6.0.3.4) lib/action_view/renderer/renderer.rb:53:in `render_partial'
actionview (6.0.3.4) lib/action_view/helpers/rendering_helper.rb:38:in `render'
app/views/admin/cards/edit.html.erb:35
actionview (6.0.3.4) lib/action_view/base.rb:274:in `_run'
actionview (6.0.3.4) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.3.4) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.3.4) lib/action_view/template.rb:385:in `instrument_render_template'
actionview (6.0.3.4) lib/action_view/template.rb:183:in `render'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:58:in `block (2 levels) in render_template'
actionview (6.0.3.4) lib/action_view/renderer/abstract_renderer.rb:88:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.3.4) lib/action_view/renderer/abstract_renderer.rb:87:in `instrument'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:57:in `block in render_template'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:65:in `render_with_layout'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:56:in `render_template'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:13:in `render'
actionview (6.0.3.4) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
actionview (6.0.3.4) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
actionview (6.0.3.4) lib/action_view/rendering.rb:117:in `block in _render_template'
actionview (6.0.3.4) lib/action_view/base.rb:304:in `in_rendering_context'
actionview (6.0.3.4) lib/action_view/rendering.rb:116:in `_render_template'
actionpack (6.0.3.4) lib/action_controller/metal/streaming.rb:218:in `_render_template'
actionview (6.0.3.4) lib/action_view/rendering.rb:103:in `render_to_body'
actionpack (6.0.3.4) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.0.3.4) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.0.3.4) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.0.3.4) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (6.0.3.4) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
/home/uelcom/.rbenv/versions/2.6.1/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
activesupport (6.0.3.4) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (6.0.3.4) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:43:in `render'
administrate (0.14.0) app/controllers/administrate/application_controller.rb:38:in `edit'
actionpack (6.0.3.4) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.0.3.4) lib/abstract_controller/base.rb:195:in `process_action'
actionpack (6.0.3.4) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.0.3.4) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.0.3.4) lib/active_support/callbacks.rb:135:in `run_callbacks'
actionpack (6.0.3.4) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.0.3.4) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `instrument'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (6.0.3.4) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (6.0.3.4) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.0.3.4) lib/abstract_controller/base.rb:136:in `process'
actionview (6.0.3.4) lib/action_view/rendering.rb:39:in `process'
actionpack (6.0.3.4) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (6.0.3.4) lib/action_controller/metal.rb:254:in `dispatch'
actionpack (6.0.3.4) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (6.0.3.4) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:49:in `block in serve'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.3.4) lib/action_dispatch/routing/route_set.rb:834:in `call'
warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
warden (1.2.9) lib/warden/manager.rb:34:in `catch'
warden (1.2.9) lib/warden/manager.rb:34:in `call'
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack (2.2.3) lib/rack/conditional_get.rb:27:in `call'
rack (2.2.3) lib/rack/head.rb:12:in `call'
actionpack (6.0.3.4) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/cookies.rb:648:in `call'
activerecord (6.0.3.4) lib/active_record/migration.rb:567:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.3.4) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.3.4) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (4.1.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.1.0) lib/web_console/middleware.rb:19:in `block in call'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.3.4) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.0.3.4) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.3.4) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.3.4) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.3.4) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.3.4) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rails_same_site_cookie (0.1.8) lib/rails_same_site_cookie/middleware.rb:13:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
webpacker (4.3.0) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.3.4) lib/rails/engine.rb:527:in `call'
/usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:107:in `process_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:149:in `accept_and_process_next_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
/usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:113:in `block in create_thread_and_abort_on_exception' 

Update_Dashboards

require "administrate/base_dashboard"

class CardDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    user: Field::BelongsTo.with_options(
      searchable: true,
      searchable_fields: ['first_name', 'last_name', 'email', 'mobile']
    ),
    id: Field::Number,
    virtual: Field::Boolean,
    blocked: Field::Boolean,
    code: Field::String,
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
  }.freeze

  COLLECTION_ATTRIBUTES = %i[
  user
  id
  virtual
  blocked
  ].freeze

  SHOW_PAGE_ATTRIBUTES = %i[
  user
  id
  virtual
  blocked
  code
  created_at
  updated_at
  ].freeze

  FORM_ATTRIBUTES = %i[
  user
  virtual
  blocked
  code
  ].freeze

  COLLECTION_FILTERS = {}.freeze


  def display_resource(card)
    card.user.last_name + ', ' + card.user.first_name
  end


end

require "administrate/base_dashboard"

class UserDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    nation: Field::BelongsTo,
    card: Field::HasOne,
    notifications: Field::HasMany,
    attachments: Field::HasMany,
    id: Field::Number,
    last_user_update: Field::Number,
    last_user_update_at: Field::DateTime,
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
  }.freeze

  COLLECTION_ATTRIBUTES = %i[
  id
  email
  mobile
  login_name
  municipal
  newsletter
  ].freeze

  SHOW_PAGE_ATTRIBUTES = %i[
  nation
  card
  notifications
  attachments
  id
  email
  first_name
  last_name
  date_of_birth
  address
  storage_bin
  created_at
  updated_at
  ].freeze

  FORM_ATTRIBUTES = %i[
  nation
  card
  notifications
  attachments
  email
  mobile
  ].freeze

  COLLECTION_FILTERS = {}.freeze
    
  def display_resource(user)
      user ? user.last_name + ', ' + user.first_name : "No user selected"
  end

end
4

1 回答 1

0

你说你想在CardDashboard下面表示卡片:

card.user.last_name + ', ' + card.user.first_name

然后你使用这个代码示例,它不起作用:

def display_resource(user)
  card.user.last_name
end

我可能遗漏了一些信息,您的示例可能有错字,但我希望您拥有的是以下内容:

def display_resource(card)
  card.user.last_name
end

该方法CardDashboard#display_resource将接收卡片作为参数,而不是用户。你不能card在你的例子中引用,因为它没有定义(你的论点是user相反的)。然而,我的示例应该可以工作,尽管它将在任何地方将卡片表示为 first_name+last_name,而不仅仅是在其他仪表板中表示为关联时。

于 2021-03-11T14:30:21.263 回答