Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rails_admin and app getting signals crossed between routes tables #658

Closed
IanWhalen opened this issue Aug 11, 2011 · 5 comments
Closed

rails_admin and app getting signals crossed between routes tables #658

IanWhalen opened this issue Aug 11, 2011 · 5 comments
Assignees
Milestone

Comments

@IanWhalen
Copy link

I haven't quite hammered out if this is a rails_admin issue or simply the way rails_admin is interacting with devise, cancan and/or Rails 3.1, but I'm getting the following exception:

Started GET "/admin" for 127.0.0.1 at 2011-08-11 15:52:49 -0400
  Processing by RailsAdmin::MainController#index as HTML
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Completed 404 Not Found in 91ms

ActionController::RoutingError (No route matches {:controller=>"things"}):
  app/controllers/application_controller.rb:5:in `block in <class:ApplicationController>'

I've been able to reproduce the error within a minute or two by rebuilding an app from scratch as follows:

Run:

$ rails new test_app
$ cd test_app

Add to ./Gemfile

gem 'rails', :git => 'https://github.com/rails/rails.git', :branch => '3-1-stable'
gem 'sqlite3'
gem 'ruby-debug19', :require => 'ruby-debug'
gem 'devise'
gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'
gem 'cancan'

Run:

$ gem install devise
$ bundle install

Delete from ./config/environments/development.rb

config.action_view.debug_rjs = true

Run:

$ rails g devise:install
$ rails g devise user
$ rails g scaffold things
$ rails g cancan:ability

Add to ./config/routes.rb

mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
root :to => "things#index"

Run:

$ rake rails_admin:install

Add to ./app/controllers/application_controllerr.rb

rescue_from CanCan::AccessDenied do |exception|
  redirect_to root_url, :alert => exception.message
end

Add to ./config/initializers/rails_admin.rb

RailsAdmin.config do |config|
  config.authorize_with :cancan
end

Run:

$ rake db:migrate
$ rails s

Now go to http://localhost:3000/users/sign_up and create a new user account (not as an admin) and then go to http://localhost:3000/admin - boom, exception. Troubleshooting so far has led me to believe that the redirect_to only has access to the RailsAdmin routes table instead of the app routes table. Since no one else has filed a bug on this I guess it has something to do with the way I've set my app up, although to my knowledge I've follow the rails_admin setup instructions to the letter.

@msimkins
Copy link

I have a feeling that this is a 'mountable' engine, rather that a full engine, and so it is not getting the parent routes

I can get around the issue by doing an explicit redirect to the full url, as a string, but Im not sure that should be the right way to do it

RailsAdmin.config do |config|
  config.authorize_with do

  if Rails.env == 'development'
      @rails_host = 'http://localhost:3000'
    end

  if Rails.env == 'test'
      @rails_host = 'http://test.host:3000'
    end
    redirect_to @rails_host  unless current_user.admin?
  end
end

I will try and look at the code later to see if I am missing something.....

@msimkins
Copy link

My brain is obviously not working today....

Not sure if this will work with cancan, I don't use it on this project, but there is a 'main_app' helper, so my post above can actually just be reduced to

RailsAdmin.config do |config|
  config.authorize_with do
    redirect_to main_app.root_url, :alert =>"Sorry Not Allowed"  unless current_user.admin?
  end
end

@IanWhalen
Copy link
Author

Hey msimkins, that main_app.root_url bit is exactly what I was looking for to get me around the problem (I kept trying app.root_url, but obviously to no avail).

And although it's good to have a workaround, that still leaves me at a bit of a loss as to why this only seems to be affecting my installation. As far as I can tell I've followed most of the standard installation and setup instructions for each of these apps, and I'm not using any particularly edge versions of rails or ruby, so I'd expect that I'd see mostly the same results as other people.

@msimkins
Copy link

I certainly see it in 3.1.0.rc5, it is the first time I have used it, so I cannot be sure, but a quick look over the code looks to totally segregate the code from the enclosing app, which is why the 'main_app.root_url' works, I am not sure if this is meant to be the desired behaviour or not, as I am fairly new to the 'Engine' system

@ghost ghost assigned bbenezech Aug 20, 2011
@bbenezech
Copy link
Collaborator

It's normal behaviour. Block is eval'd in ENgine's scope, and routes are now name_scoped.
main_app.root_url is correct.
If you have a minute to document it somewhere, that would be cool.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants