igorkasyanchuk / rails_db

Rails Database Viewer and SQL Query Runner
https://www.railsjazz.com/
MIT License
1.46k stars 111 forks source link

undefined local variable or method `rails_db' #74

Closed sveredyuk closed 6 years ago

sveredyuk commented 7 years ago

Rails 5.0.2

routes.rb:

mount RailsDb::Engine => '/db', as: 'rails_db'

and visit /db

NameError at / undefined local variable or method `rails_db' for #<#:0x007fb1801caa00> Did you mean? rails_db_tables

at:

app/views/rails_db/shared/_header.html.erb

<div class="row">
  <div class="large-3 columns">
    <h1><%= link_to image_tag('rails_db/logo_mini.png'), rails_db.root_path %></h1>
  </div>
  <div class="large-9 columns">
    <ul class="inline-list left main_nav">
      <li>
        <%= link_to rails_db.root_path do %>
igorkasyanchuk commented 7 years ago

I don't know why. Build is passed (with Rails 5.0.2) https://travis-ci.org/igorkasyanchuk/rails_db/builds/210254101 provide more details.

Vaultman commented 7 years ago

This happens when the engine is mounted inside namespace.

Rails.application.routes.draw do
  namespace :admin do
    mount RailsDb::Engine, at: '/rails/db', as: 'rails_db'
  end

  # Other routes...
end

Visit to /admin/rails/db results to undefined local variable or method 'rails_db'

bbenezech commented 7 years ago

It makes sense. You can use scope :admin, but then it is equivalent to

mount RailsDb::Engine, at: '/admin/rails/db', as: 'rails_db'

It should work. I don't think this is a bug in the Rails Engine sense.

bbenezech commented 7 years ago

@igorkasyanchuk Maybe it is. I don't think you are supposed to namespace your own routes, just the main_app ones. Do you remember why you did that ?

igorkasyanchuk commented 7 years ago

@bbenezech not sure. maybe because of some other issue.

igorkasyanchuk commented 6 years ago

@sveredyuk check again with v 2.0. closing for now

airhorns commented 6 years ago

This is still happening for me when mounting inside a namespace on version 2.0.1. I'd love to be able to use a namespace because it allows using one way to authenticate all the tools, like this:

  namespace :admin do
    resources :posts
    # ... 

    namespace :tools do
      mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
      mount RailsDb::Engine => '/db', :as => 'rails_db'
      mount Sidekiq::Web => '/sidekiq'
    end
  end
igorkasyanchuk commented 6 years ago

@airhorns very strange, here is a test which I've added: https://github.com/igorkasyanchuk/rails_db/commit/55b336571441f27132928afcba74306d5925de90

also tried with Rails 5.1 app and it was working.

I suggest you to make a fork, clone it and connect in your app in Gemfile:

gem 'rails_db', path: '../rails_db'

then you can make a modifications in gem and see if it works

airhorns commented 6 years ago

Played around a bit and I can't get my local version to work, but I think I know why your test is passing and you can't reproduce. The error I get is this:

web_1                     | ActionView::Template::Error (undefined local variable or method `rails_db' for #<#<Class:0x00007f2fd4b35dc0>:0x0000564130f4e878>):
web_1                     |     1: <div class="row">
web_1                     |     2:   <div class="large-3 columns">
web_1                     |     3:     <h1><%= link_to image_tag('rails_db/logo_mini.png'), rails_db.root_path %></h1>
web_1                     |     4:   </div>
web_1                     |     5:   <div class="large-9 columns">
web_1                     |     6:     <ul class="inline-list left main_nav">
web_1                     |
web_1                     | /usr/local/bundle/bundler/gems/rails_db-55b336571441views/rails_db/shared/_header.html.erb:3:in `__usr_local_bundle_bundler_gems_rails_db___b_________views_rails_db_shared__header_html_erb___3767413112093266131_69921719566480'
web_1                     | actionview (5.2.0) lib/action_view/template.rb:159:in `block in render'

from trying to use the rails_db object (which is a ActionDispatch::Routing::RoutesProxy) to generate routes within the engine. When you mount RailsDb at a different place, that RoutesProxy object doesn't exist at rails_db, hence the nil. In your test however, RailsDb is mounted as it would normally be at the root of the routes system as well. There is more than one instance of it. The normally mounted one creates that rails_db routes proxy, but for a different instance of RailsDb in the test environment. I would bet that the links rendered in that environment at /x/y/db don't link to assets at /x/y/db but instead at /rails_db, because it's using the route proxy of the other instance.

igorkasyanchuk commented 6 years ago

@airhorns thanks for the deep analysis, and yes - by removing root "rails_db" from routes I was able to reproduce the issue.

I've prepared a fix in the master branch. Could you please check it https://github.com/igorkasyanchuk/rails_db/commit/e0b9f0eae71d551d8542a53fba4ead7ef4d74c49 ?

Try to point rails_db in your project to Github, master branch. If it works - I'll release a new version of the gem.

gem "rails_db", github: 'igorkasyanchuk/rails_db'

igorkasyanchuk commented 6 years ago

actually ... I made a new release since app tests are passed. Try the latest version of gem.

airhorns commented 6 years ago

That fixed it for me! Thanks @igorkasyanchuk !

airhorns commented 6 years ago

Ah, I spoke too soon @igorkasyanchuk , apologies. The index page for rails_db rendered fine, but all the functionality is still broken after your most recent patch. If I try to visit the SQL Editor, I still get a

undefined local variable or method `admin_tools_rails_db_rails_db' for #<#<Class:0x000056187eb350c8>:0x00007f8d1c0a29d0>

when mounting at admin/tools/rails_db.

Full trace:

web_1                     | ActionView::Template::Error (undefined local variable or method `admin_tools_rails_db_rails_db' for #<#<Class:0x00007f3460a1b940>:0x00007f3454cebb68>):
web_1                     |     1: <div class="row">
web_1                     |     2:   <div class="large-3 columns">
web_1                     |     3:     <h1><%= link_to image_tag('rails_db/logo_mini.png'), rails_db.root_path %></h1>
web_1                     |     4:   </div>
web_1                     |     5:   <div class="large-9 columns">
web_1                     |     6:     <ul class="inline-list left main_nav">
web_1                     |
web_1                     | /usr/local/bundle/bundler/gems/rails_db-e5a5de9f3204helpers/rails_db/application_helper.rb:16:in `eval'
web_1                     | /usr/local/bundle/bundler/gems/rails_db-e5a5de9f3204helpers/rails_db/application_helper.rb:16:in `eval'
web_1                     | /usr/local/bundle/bundler/gems/rails_db-e5a5de9f3204helpers/rails_db/application_helper.rb:16:in `guess_name'
web_1                     | /usr/local/bundle/bundler/gems/rails_db-e5a5de9f3204helpers/rails_db/application_helper.rb:7:in `rescue in rails_db'
web_1                     | /usr/local/bundle/bundler/gems/rails_db-e5a5de9f3204helpers/rails_db/application_helper.rb:4:in `rails_db'
web_1                     | /usr/local/bundle/bundler/gems/rails_db-e5a5de9f3204views/rails_db/shared/_header.html.erb:3:in `__usr_local_bundle_bundler_gems_rails_db_e_a_de_f____views_rails_db_shared__header_html_erb___2294449927786753839_69931530745440'
web_1                     | actionview (5.2.0) lib/action_view/template.rb:159:in `block in render'
web_1                     | activesupport (5.2.0) lib/active_support/notifications.rb:170:in `instrument'
web_1                     | actionview (5.2.0) lib/action_view/template.rb:354:in `instrument_render_template'
web_1                     | actionview (5.2.0) lib/action_view/template.rb:157:in `render'
web_1                     | actionview (5.2.0) lib/action_view/renderer/partial_renderer.rb:344:in `block in render_partial'
web_1                     | actionview (5.2.0) lib/action_view/renderer/abstract_renderer.rb:44:in `block in instrument'
web_1                     | activesupport (5.2.0) lib/active_support/notifications.rb:168:in `block in instrument'
web_1                     | activesupport (5.2.0) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
web_1                     | activesupport (5.2.0) lib/active_support/notifications.rb:168:in `instrument'
web_1                     | actionview (5.2.0) lib/action_view/renderer/abstract_renderer.rb:43:in `instrument'
web_1                     | actionview (5.2.0) lib/action_view/renderer/partial_renderer.rb:333:in `render_partial'
web_1                     | actionview (5.2.0) lib/action_view/renderer/partial_renderer.rb:312:in `render'
web_1                     | actionview (5.2.0) lib/action_view/renderer/renderer.rb:49:in `render_partial'
web_1                     | actionview (5.2.0) lib/action_view/helpers/rendering_helper.rb:37:in `render'
web_1                     | /usr/local/bundle/bundler/gems/rails_db-e5a5de9f3204views/layouts/rails_db/application.html.erb:16:in `__usr_local_bundle_bundler_gems_rails_db_e_a_de_f____views_layouts_rails_db_application_html_erb__1461276006832306760_69931368986100'
web_1                     | actionview (5.2.0) lib/action_view/template.rb:159:in `block in render'
web_1                     | activesupport (5.2.0) lib/active_support/notifications.rb:170:in `instrument'
web_1                     | actionview (5.2.0) lib/action_view/template.rb:354:in `instrument_render_template'
web_1                     | actionview (5.2.0) lib/action_view/template.rb:157:in `render'
web_1                     | actionview (5.2.0) lib/action_view/renderer/template_renderer.rb:66:in `render_with_layout'
web_1                     | actionview (5.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
web_1                     | actionview (5.2.0) lib/action_view/renderer/template_renderer.rb:16:in `render'
web_1                     | actionview (5.2.0) lib/action_view/renderer/renderer.rb:44:in `render_template'
web_1                     | actionview (5.2.0) lib/action_view/renderer/renderer.rb:25:in `render'
web_1                     | actionview (5.2.0) lib/action_view/rendering.rb:103:in `_render_template'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/streaming.rb:219:in `_render_template'
web_1                     | actionview (5.2.0) lib/action_view/rendering.rb:84:in `render_to_body'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
web_1                     | actionpack (5.2.0) lib/abstract_controller/rendering.rb:25:in `render'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/rendering.rb:36:in `render'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
web_1                     | activesupport (5.2.0) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
web_1                     | /usr/local/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
web_1                     | activesupport (5.2.0) lib/active_support/core_ext/benchmark.rb:14:in `ms'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:46:in `block in render'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
web_1                     | activerecord (5.2.0) lib/active_record/railties/controller_runtime.rb:31:in `cleanup_view_runtime'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:45:in `render'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
web_1                     | actionpack (5.2.0) lib/abstract_controller/base.rb:194:in `process_action'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/rendering.rb:30:in `process_action'
web_1                     | actionpack (5.2.0) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
web_1                     | activesupport (5.2.0) lib/active_support/callbacks.rb:132:in `run_callbacks'
web_1                     | actionpack (5.2.0) lib/abstract_controller/callbacks.rb:41:in `process_action'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/rescue.rb:22:in `process_action'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
web_1                     | activesupport (5.2.0) lib/active_support/notifications.rb:168:in `block in instrument'
web_1                     | activesupport (5.2.0) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
web_1                     | activesupport (5.2.0) lib/active_support/notifications.rb:168:in `instrument'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
web_1                     | actionpack (5.2.0) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
web_1                     | activerecord (5.2.0) lib/active_record/railties/controller_runtime.rb:24:in `process_action'
web_1                     | actionpack (5.2.0) lib/abstract_controller/base.rb:134:in `process'
web_1                     | actionview (5.2.0) lib/action_view/rendering.rb:32:in `process'
web_1                     | actionpack (5.2.0) lib/action_controller/metal.rb:191:in `dispatch'
web_1                     | actionpack (5.2.0) lib/action_controller/metal.rb:252:in `dispatch'
web_1                     | actionpack (5.2.0) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
web_1                     | actionpack (5.2.0) lib/action_dispatch/routing/route_set.rb:34:in `serve'
web_1                     | actionpack (5.2.0) lib/action_dispatch/journey/router.rb:52:in `block in serve'
web_1                     | actionpack (5.2.0) lib/action_dispatch/journey/router.rb:35:in `each'
web_1                     | actionpack (5.2.0) lib/action_dispatch/journey/router.rb:35:in `serve'
web_1                     | actionpack (5.2.0) lib/action_dispatch/routing/route_set.rb:840:in `call'
web_1                     | railties (5.2.0) lib/rails/engine.rb:524:in `call'
web_1                     | railties (5.2.0) lib/rails/railtie.rb:190:in `public_send'
web_1                     | railties (5.2.0) lib/rails/railtie.rb:190:in `method_missing'
web_1                     | actionpack (5.2.0) lib/action_dispatch/routing/mapper.rb:19:in `block in <class:Constraints>'
web_1                     | actionpack (5.2.0) lib/action_dispatch/routing/mapper.rb:48:in `serve'
web_1                     | actionpack (5.2.0) lib/action_dispatch/journey/router.rb:52:in `block in serve'
web_1                     | actionpack (5.2.0) lib/action_dispatch/journey/router.rb:35:in `each'
web_1                     | actionpack (5.2.0) lib/action_dispatch/journey/router.rb:35:in `serve'
web_1                     | actionpack (5.2.0) lib/action_dispatch/routing/route_set.rb:840:in `call'
web_1                     | rack (2.0.5) lib/rack/tempfile_reaper.rb:15:in `call'
web_1                     | rack (2.0.5) lib/rack/etag.rb:25:in `call'
web_1                     | rack (2.0.5) lib/rack/conditional_get.rb:25:in `call'
web_1                     | rack (2.0.5) lib/rack/head.rb:12:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
web_1                     | rack (2.0.5) lib/rack/session/abstract/id.rb:232:in `context'
web_1                     | rack (2.0.5) lib/rack/session/abstract/id.rb:226:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/cookies.rb:670:in `call'
web_1                     | activerecord (5.2.0) lib/active_record/migration.rb:559:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
web_1                     | activesupport (5.2.0) lib/active_support/callbacks.rb:98:in `run_callbacks'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
web_1                     | web-console (3.6.2) lib/web_console/middleware.rb:135:in `call_app'
web_1                     | web-console (3.6.2) lib/web_console/middleware.rb:30:in `block in call'
web_1                     | web-console (3.6.2) lib/web_console/middleware.rb:20:in `catch'
web_1                     | web-console (3.6.2) lib/web_console/middleware.rb:20:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
web_1                     | railties (5.2.0) lib/rails/rack/logger.rb:38:in `call_app'
web_1                     | railties (5.2.0) lib/rails/rack/logger.rb:26:in `block in call'
web_1                     | activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `block in tagged'
web_1                     | activesupport (5.2.0) lib/active_support/tagged_logging.rb:28:in `tagged'
web_1                     | activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `tagged'
web_1                     | railties (5.2.0) lib/rails/rack/logger.rb:26:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/request_id.rb:27:in `call'
web_1                     | rack (2.0.5) lib/rack/method_override.rb:22:in `call'
web_1                     | rack (2.0.5) lib/rack/runtime.rb:22:in `call'
web_1                     | activesupport (5.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
web_1                     | actionpack (5.2.0) lib/action_dispatch/middleware/static.rb:127:in `call'
web_1                     | rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
web_1                     | webpacker (3.5.3) lib/webpacker/dev_server_proxy.rb:22:in `perform_request'
web_1                     | rack-proxy (0.6.4) lib/rack/proxy.rb:57:in `call'
web_1                     | railties (5.2.0) lib/rails/engine.rb:524:in `call'
web_1                     | puma (3.11.4) lib/puma/configuration.rb:225:in `call'
web_1                     | puma (3.11.4) lib/puma/server.rb:632:in `handle_request'
web_1                     | puma (3.11.4) lib/puma/server.rb:446:in `process_client'
web_1                     | puma (3.11.4) lib/puma/server.rb:306:in `block in run'
web_1                     | puma (3.11.4) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
igorkasyanchuk commented 6 years ago

@airhorns please try to use "master". If it works for you. If it works - I'll make a new release.

airhorns commented 6 years ago

It works great! Able to browse my schema and execute SQL. Thanks @igorkasyanchuk !

igorkasyanchuk commented 6 years ago

@airhorns cool. use now version 2.0.3