thoughtbot / administrate

A Rails engine that helps you put together a super-flexible admin dashboard.
http://administrate-demo.herokuapp.com
MIT License
5.9k stars 1.13k forks source link

How to add a new model to the Administrate framework? #756

Closed gjappleton closed 5 years ago

gjappleton commented 7 years ago

Hello! Hoping someone can help me out.

What are the steps to adding a new model to the administrate framework?

I'm hitting a: NoMethodError at /admin/tags undefined method `size' for nil:NilClass error as a result of not adding my 'tags' model properly. I've added a tags_controller under admin, a tag resource under admin route in routes.rb, and a tag_dashboard.rb in dashboard folder.

Thank you!

gjappleton commented 7 years ago

Note: https://github.com/thoughtbot/administrate/issues/182 doesn't have the info i'm looking for as I've already created the files it suggests. One hint I think is looking into the dashboard manifest, but at this juncture I am still figuring out what it does//how to even access it.

gjappleton commented 7 years ago

Started POST "/__better_errors/2445b3021c0baf92/variables" for ::1 at 2017-02-23 13:18:35 -0500 Started GET "/admin/tags" for ::1 at 2017-02-23 13:19:11 -0500 Processing by Admin::TagsController#index as HTML Rendering /Users/gabrielappleton/.rvm/gems/ruby-2.3.3/gems/administrate-0.3.0/app/views/administrate/application/index.html.erb within layouts/administrate/application Rendered /Users/gabrielappleton/.rvm/gems/ruby-2.3.3/gems/administrate-0.3.0/app/views/administrate/application/_search.html.erb (3.8ms) Tag Load (0.4ms) SELECT "tags". FROM "tags" LIMIT $1 OFFSET $2 [["LIMIT", 20], ["OFFSET", 0]] Category Load (0.5ms) SELECT "categories". FROM "categories" WHERE "categories"."id" = $1 ORDER BY categories.created_at DESC LIMIT $2 [["id", 2], ["LIMIT", 1]] (0.7ms) SELECT COUNT(*) FROM "taggings" WHERE "taggings"."tag_id" = $1 [["tag_id", 1]] Rendered /Users/gabrielappleton/.rvm/gems/ruby-2.3.3/gems/administrate-0.3.0/app/views/fields/has_many/_index.html.erb (2.7ms) Rendered /Users/gabrielappleton/.rvm/gems/ruby-2.3.3/gems/administrate-0.3.0/app/views/fields/has_many/_index.html.erb (7.1ms) Rendered /Users/gabrielappleton/.rvm/gems/ruby-2.3.3/gems/administrate-0.3.0/app/views/administrate/application/_collection.html.erb (35.4ms) Rendered /Users/gabrielappleton/.rvm/gems/ruby-2.3.3/gems/administrate-0.3.0/app/views/administrate/application/index.html.erb within layouts/administrate/application (76.6ms) Completed 500 Internal Server Error in 122ms (ActiveRecord: 1.6ms)

DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from initialize at /Users/gabrielappleton/.rvm/gems/ruby-2.3.3/gems/better_errors-2.1.1/lib/better_errors/raised_exception.rb:7) DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from initialize at /Users/gabrielappleton/.rvm/gems/ruby-2.3.3/gems/better_errors-2.1.1/lib/better_errors/raised_exception.rb:8)

NoMethodError - undefined method size' for nil:NilClass: administrate (0.3.0) app/views/fields/has_many/_index.html.erb:19:in_sers_gabrielappletonrvm_gems_ruby_gems_administrate___app_views_fields_has_many__index_htmlerb727911858109318415_70337803785420' actionview (5.0.0.1) lib/action_view/template.rb:158:in block in render' activesupport (5.0.0.1) lib/active_support/notifications.rb:166:ininstrument' actionview (5.0.0.1) lib/action_view/template.rb:348:in instrument' actionview (5.0.0.1) lib/action_view/template.rb:156:inrender' actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:343:in render_partial' actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:311:inblock in render' actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in block in instrument' activesupport (5.0.0.1) lib/active_support/notifications.rb:164:inblock in instrument' activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in instrument' activesupport (5.0.0.1) lib/active_support/notifications.rb:164:ininstrument' actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in instrument' actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:310:inrender' actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in render_partial' actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:21:inrender' actionview (5.0.0.1) lib/action_view/helpers/rendering_helper.rb:32:in render' administrate (0.3.0) app/helpers/administrate/application_helper.rb:5:inrender_field' administrate (0.3.0) app/views/administrate/application/_collection.html.erb:63:in block (2 levels) in ___sers_gabrielappleton__rvm_gems_ruby_______gems_administrate_______app_views_administrate_application__collection_html_erb___3867413717467151431_70337791459520' administrate (0.3.0) app/views/administrate/application/_collection.html.erb:61:inblock in _sers_gabrielappletonrvm_gems_ruby_gems_administrate___app_views_administrate_applicationcollection_html_erb_3867413717467151431_70337791459520' activerecord (5.0.0.1) lib/active_record/relation/delegation.rb:38:in each' administrate (0.3.0) app/views/administrate/application/_collection.html.erb:55:in___sers_gabrielappletonrvm_gems_ruby_gems_administrate___app_views_administrate_applicationcollection_html_erb_3867413717467151431_70337791459520' actionview (5.0.0.1) lib/action_view/template.rb:158:in block in render' activesupport (5.0.0.1) lib/active_support/notifications.rb:166:ininstrument' actionview (5.0.0.1) lib/action_view/template.rb:348:in instrument' actionview (5.0.0.1) lib/action_view/template.rb:156:inrender' actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:343:in render_partial' actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:311:inblock in render' actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in block in instrument' activesupport (5.0.0.1) lib/active_support/notifications.rb:164:inblock in instrument' activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in instrument' activesupport (5.0.0.1) lib/active_support/notifications.rb:164:ininstrument' actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in instrument' actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:310:inrender' actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in render_partial' actionview (5.0.0.1) lib/action_view/helpers/rendering_helper.rb:35:inrender' administrate (0.3.0) app/views/administrate/application/index.html.erb:43:in `___sers_gabrielappletonrvm_gems_ruby_gems_administrate___app_views_administrate_application_index_htmlerb3764132621317459412_70337790746060' actionview (5.0.0.1) lib/action_view/template.rb:158:in block in render' activesupport (5.0.0.1) lib/active_support/notifications.rb:166:ininstrument' actionview (5.0.0.1) lib/action_view/template.rb:348:in instrument' actionview (5.0.0.1) lib/action_view/template.rb:156:inrender' actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:54:in block (2 levels) in render_template' actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:inblock in instrument' activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in block in instrument' activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:ininstrument' activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in instrument' actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:ininstrument' actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:53:in block in render_template' actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:61:inrender_with_layout' actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:52:in render_template' actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:14:inrender' actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:42:in render_template' actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:23:inrender' actionview (5.0.0.1) lib/action_view/rendering.rb:103:in _render_template' actionpack (5.0.0.1) lib/action_controller/metal/streaming.rb:217:in_render_template' actionview (5.0.0.1) lib/action_view/rendering.rb:83:in render_to_body' actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:52:inrender_to_body' actionpack (5.0.0.1) lib/action_controller/metal/renderers.rb:144:in render_to_body' actionpack (5.0.0.1) lib/abstract_controller/rendering.rb:26:inrender' actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:36:in render' actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:44:inblock (2 levels) in render' activesupport (5.0.0.1) lib/active_support/core_ext/benchmark.rb:12:in block in ms' /Users/gabrielappleton/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/benchmark.rb:308:inrealtime' activesupport (5.0.0.1) lib/active_support/core_ext/benchmark.rb:12:in ms' actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:44:inblock in render' actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:87:in cleanup_view_runtime' activerecord (5.0.0.1) lib/active_record/railties/controller_runtime.rb:25:incleanup_view_runtime' actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:43:in render' administrate (0.3.0) app/controllers/administrate/application_controller.rb:12:inindex' actionpack (5.0.0.1) lib/action_controller/metal/basic_implicit_render.rb:4:in send_action' actionpack (5.0.0.1) lib/abstract_controller/base.rb:188:inprocess_action' actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:30:in process_action' actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:20:inblock in process_action' activesupport (5.0.0.1) lib/active_support/callbacks.rb:126:in call' activesupport (5.0.0.1) lib/active_support/callbacks.rb:506:inblock (2 levels) in compile' activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:in call' activesupport (5.0.0.1) lib/active_support/callbacks.rb:101:inrun_callbacks__' activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in _run_process_action_callbacks' activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:inrun_callbacks' actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:19:in process_action' actionpack (5.0.0.1) lib/action_controller/metal/rescue.rb:20:inprocess_action' actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:32:in block in process_action' activesupport (5.0.0.1) lib/active_support/notifications.rb:164:inblock in instrument' activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in instrument' activesupport (5.0.0.1) lib/active_support/notifications.rb:164:ininstrument' actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:30:in process_action' actionpack (5.0.0.1) lib/action_controller/metal/params_wrapper.rb:248:inprocess_action' activerecord (5.0.0.1) lib/active_record/railties/controller_runtime.rb:18:in process_action' actionpack (5.0.0.1) lib/abstract_controller/base.rb:126:inprocess' actionview (5.0.0.1) lib/action_view/rendering.rb:30:in process' actionpack (5.0.0.1) lib/action_controller/metal.rb:190:indispatch' actionpack (5.0.0.1) lib/action_controller/metal.rb:262:in dispatch' actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:50:indispatch' actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:32:in serve' actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:39:inblock in serve' actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in serve' actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:725:incall' warden (1.2.6) lib/warden/manager.rb:35:in block in call' warden (1.2.6) lib/warden/manager.rb:34:incall' rack (2.0.1) lib/rack/etag.rb:25:in call' rack (2.0.1) lib/rack/conditional_get.rb:25:incall' rack (2.0.1) lib/rack/head.rb:12:in call' rack (2.0.1) lib/rack/session/abstract/id.rb:222:incontext' rack (2.0.1) lib/rack/session/abstract/id.rb:216:in call' actionpack (5.0.0.1) lib/action_dispatch/middleware/cookies.rb:613:incall' activerecord (5.0.0.1) lib/active_record/migration.rb:552:in call' actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:inblock in call' activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `run_callbacks__' activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in _run_call_callbacks' activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:inrun_callbacks' actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in call' actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:incall' actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in call' better_errors (2.1.1) lib/better_errors/middleware.rb:84:inprotected_app_call' better_errors (2.1.1) lib/better_errors/middleware.rb:79:in better_errors_call' better_errors (2.1.1) lib/better_errors/middleware.rb:57:incall' actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in call' web-console (3.4.0) lib/web_console/middleware.rb:135:incall_app' web-console (3.4.0) lib/web_console/middleware.rb:28:in block in call' web-console (3.4.0) lib/web_console/middleware.rb:18:incall' actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in call' railties (5.0.0.1) lib/rails/rack/logger.rb:36:incall_app' railties (5.0.0.1) lib/rails/rack/logger.rb:24:in block in call' activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:inblock in tagged' activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in tagged' activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:intagged' railties (5.0.0.1) lib/rails/rack/logger.rb:24:in call' sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:incall' actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in call' rack (2.0.1) lib/rack/method_override.rb:22:incall' rack (2.0.1) lib/rack/runtime.rb:22:in call' activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:incall' actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in call' actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:incall' rack (2.0.1) lib/rack/sendfile.rb:111:in call' railties (5.0.0.1) lib/rails/engine.rb:522:incall' puma (3.6.2) lib/puma/configuration.rb:225:in call' puma (3.6.2) lib/puma/server.rb:578:inhandle_request' puma (3.6.2) lib/puma/server.rb:415:in process_client' puma (3.6.2) lib/puma/server.rb:275:inblock in run' puma (3.6.2) lib/puma/thread_pool.rb:116:in `block in spawn_thread'

Started POST "/__better_errors/f43b5c3706bbe7bf/variables" for ::1 at 2017-02-23 13:19:11 -0500

gjappleton commented 7 years ago

nomethoderror_at__admin_tags

gjappleton commented 7 years ago

anyone??? :-(

carlosramireziii commented 7 years ago

Hey @gjappleton! Sorry to the delayed response.

I know you already said #182 does not have what you are looking for because you already have those files. But did you try removing the files you created and letting the generator generate them for you? There may be a difference between how you created the files and how the generator creates them.

If that doesn't work, it might be helpful to link a gist that has all the relevant files you created so that maybe someone can spot the problem.

gjappleton commented 7 years ago

hi @carlosramireziii ! yes, i did let the generator generate them. what i did was:

  1. rails generate administrate:dashboard Tag
  2. added resource to the routes

and that gets me to hitting the error I posted. I can look into posting a gist with all the relevant files if nothing else comes to mind at the moment for you .

thanks for the response!

carlosramireziii commented 7 years ago

A gist would be great!

gjappleton commented 7 years ago

@carlosramireziii here's a gist with the controller, dashboard from adminstrate as well as the routes file. Should i add anything else?

https://gist.github.com/gjappleton/ec98c83f6ff5c946d12c289c27bf9bda

jetsgit commented 7 years ago

...umm, I'm assuming you did not REALLY do:

rails generate administrate:dashboard Foo

but really did:

rails generate administrate:dashboard Tag

Best to explain exactly what you really did, not use generic example as at times people are actually using a generic example by mistake. Also, if you would do:

rails generate administrate:dashboard Tag > generated_output.txt

you would create useful information of what files are being generated in the file generated_output.txt which would be useful in a gist.

carlosramireziii commented 7 years ago

This line is jumping out at me as something to look into

influencers: Field::HasMany.with_options(class_name: "Influencers")

What was your intention with using the class_name option? Is the class name definitely Influencers? If you temporary remove that field from your dashboard (remove occurrences of :influencers from the dashboard), does it work?

I would take a look at that and report back.

carlosramireziii commented 7 years ago

@gjappleton Just seeing if there were any updates on this. Let us know! Thanks

dfwmountainbiker commented 5 years ago

For any that find this, if you already have an existing Administrate dashboard:

New models will not show up in the side list until you add them to the routes; for details look wherever your gems are installled, as it references:

gems/administrate-0.10.0/app/views/administrate/application/_navigation.html.erb

frm commented 5 years ago

Thanks @dfwmountainbiker !

For future reference, this is also available in the getting started docs. Closing it 👍