1

I want to make all generated URLs can start with /sample_1/ as its prefix.

Because the server is running under subfolder,

how could I do on Rails 4.2.4 ? The following routes rules don't generate all paths with the prefix.

I couldn't find any workable solution by Google

routes.rb

Rails.application.routes.draw do

  sample_1 = Proc.new do
      root 'welcome#category'
  end

  if ENV['RAILS_RELATIVE_URL_ROOT']
    scope ENV['RAILS_RELATIVE_URL_ROOT'] do
      sample_1.call
    end
  else
    sample_1.call
  end

end

Application.rb

module Sample
  class Application < Rails::Application

    config.active_record.raise_in_transactional_callbacks = true
    config.middleware.use Rack::Deflater
    config.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']
  end
end

nginx , the root URL runs flask web server not rails

    location /sample_1/ {
        rewrite ^/sample_1/(.*)$ /$1 break;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   Host $http_host;
        proxy_pass http://127.0.0.1:8512;
    }

    location / {
            proxy_pass http://localhost:8006;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
user3675188
  • 7,271
  • 11
  • 40
  • 76
  • Rails should actually prefix your routes if your have set the RAILS_RELATIVE_URL_ROOT env var. However I tested it and it does not work as documented on http://guides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root – max Oct 26 '15 at 11:33
  • In your application.rb do you have this set: ``config.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']`` – DustinFisher Oct 26 '15 at 11:39
  • @max I did it. but it didn't work – user3675188 Oct 26 '15 at 11:47
  • You don't actually need to set `config.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']`. Rails will do that, the problem is that `config.relative_url_root` does not seem to do anything. https://github.com/rails/rails/issues/22074 – max Oct 26 '15 at 11:53
  • @max anyway this line won't do any hamful thing isn't it ? so, what the generated urls still don't have the prefix `sample_1`, any idea? – user3675188 Oct 26 '15 at 11:55
  • No, its not harmfull, but it does no good at all. I would do a workaround just like you have but I would use a different ENV var than the one Rails uses such as `MYAPP_RELATIVE_URL_ROOT `. That way if the issue is fixed your application will not break just by upgrading Rails. – max Oct 26 '15 at 12:14

3 Answers3

0

did you try to prefix using :as with scope:

scope "admin", :as => "admin" do
  resources :photos, :accounts
end

resources :photos, :accounts

This will generate routes such as admin_photos_path and admin_accounts_path which map to /admin/photos and /admin/accounts respectively.

:as Prefixes the routing helpers in this scope with the specified label.

  scope :as => "sekret" do
    resources :posts
  end
Shiva
  • 11,485
  • 2
  • 67
  • 84
  • I think he is looking to mount the same app in different folders on a web server. Using `as` does not really make sense in that case as he is not talking about prefixing the helpers - just the URL's. – max Oct 26 '15 at 12:32
  • it not just prefix the helpers also the URL to access has `prefex/namespace`; anyway if he says this answer is irrelevant then I must delete this @max; – Shiva Oct 26 '15 at 12:46
  • as first the root_path will generate `/` , but what i expect is root_path can generate '/prefix_1' , NOT `prefix_1_root_path` – user3675188 Oct 26 '15 at 14:23
0

Since the built in Rails relative_url_root option does not seem to work, you can use a workaround:

Rails.application.routes.draw do
  # Bad hack because relative_url_root does not work.
  # @fixme, @todo
  scope ENV['MYAPP_RELATIVE_URL_ROOT'] do
   root 'welcome#category'
  end
end

Calling scope nil actually does nothing to the resulting routes so you can simplify quite a bit.

$ rake routes MYAPP_RELATIVE_URL_ROOT=foo
Prefix Verb URI Pattern    Controller#Action
  root GET  /foo(.:format) welcome#category

I would not use RAILS_RELATIVE_URL_ROOT in the workaround since your app will unexpectedly break if the behavior is fixed in future Rails release. So a safe approach is to leave RAILS_RELATIVE_URL_ROOT unset until you know it is fixed and then remove the hack.

Edited.

It does work. You are most likely doing something else wrong.

screenshot

  1. git clone https://github.com/maxcal/sandbox/tree/33344204
  2. cd sandbox
  3. bundle install
  4. MYAPP_RELATIVE_URL_ROOT=test rails s
  5. open localhost:3000/test in your browser.

How you actually set ENV vars depends on your shell. The above works for Bash/Zsh. I'm guessing that this is where you are failing.

max
  • 96,212
  • 14
  • 104
  • 165
  • it didn't work too!!!! i can get the expected routes only by `rake routes`. be the generated URLs like, product_path, root_path, still not working! – user3675188 Oct 26 '15 at 14:21
  • And make sure you restart your Rails server! – max Oct 26 '15 at 15:11
0

The Rails docs say you can use a scope:

#config/routes.rb
def method
   root "....."
   resources :controller_1, :controller_2
end

if .......
   scope :prefix_1 do
      method
   end
else
   method
end

Because the server is running under subfolder

This should not be a problem for Rails.

If you want the app in a subfolder, it's under the duress of your web server software to route it.

Here's an example with nginx:

server {
   listen 80;
   server_name your_folder.com;
   root /your/app/destination/folder;

This means if you stick to using the _path helpers (not _url), it will keep all the relative paths working properly.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • i've already done what you said, but it didn't work on generated relative URLs, like product_path should generate `prefix_1/product/:id` but it still generates `product/:id`. that's the problem i'm handling – user3675188 Oct 26 '15 at 14:25
  • Thanks ~~ I think I got a strange problem, i mus set somthing wrong – user3675188 Oct 26 '15 at 14:46
  • Sir, could you check my lastest update ? i put the nginx config – user3675188 Oct 26 '15 at 14:49