Running Rails+Passenger+Devise from a subdirectory?
Asked Answered
N

2

12

I have a server A which proxies all traffic on /rails to server B.

So I setup this virtual host, and most things work...okay. link_to is broken and generates urls to /users as opposed to /rails/users, but I can work around that.

If I set config.action_controller.relative_url_root to /rails then my routes work okay EXCEPT all the devise routes. They point to the bare URL. How do I properly configure server B to understand that its running in a subdirectory and generate links and routes correctly?

<VirtualHost *:80>
    ServerName http://ec2-url.compute-1.amazonaws.com/
    SetEnv RDS_HOSTNAME "mydb..."
    SetEnv RAILS_RELATIVE_URL_ROOT "/rails"

    DocumentRoot /home/ubuntu/myapp/public
    RailsEnv staging 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/app.log combined
    PassengerLogLevel 3
    <Directory "/home/ubuntu/myapp/public">
 Options FollowSymLinks
   AllowOverride None
   Order allow,deny
   Allow from all
   Options -MultiViews
   Require all granted
    </Directory>
</VirtualHost>

I am using Rails 4.

Norikonorina answered 10/4, 2015 at 19:50 Comment(1)
what version of rails are you using?Amur
N
5

In your environment files, add a config for OmniAuth.config.full_host.

OmniAuth.config.full_host = 'http://myfullurl/subdir'

Now, in application_controller.rb, add this method:

def after_sign_in_path_for(resource_or_scope)
    path = super(resource_or_scope)
    "#{OmniAuth.config.full_host}#{path}"
end
Norikonorina answered 17/4, 2015 at 16:55 Comment(0)
A
0

Would be great if you share your routes.rb, but I think the easy way to change devise routes is to put something like the following in routes.rb

scope '/rails' do
  devise_for :users
end
Anomalism answered 16/4, 2015 at 3:40 Comment(4)
This creates routes like /rails/railsNorikonorina
I don't understand. First you say that devise is creating routes like '/' (they point to bare URL). And with scope it generates a double scope???Anomalism
@StefanKendall Try the following: quickhack.net/nom/blog/… is in japanese but the examples are in plain englishAnomalism
I had just that and link_to and the callback URLs were still wrong. I either get /callback or /rails/rails/callback. Setting OmniAuth.config.full_host and manually crafting links was the only way I got the devise callbacks to work, and then I could manually create the functional route urls for /rails/callbackNorikonorina

© 2022 - 2024 — McMap. All rights reserved.