Rails 3 legacy “assets” routes

2019-08-13 19:52发布

问题:

I'm in the process of upgrading from a Rails 2.3.14 app to Rails 3.2.1 So far, the only issue is an inability to specify routes to our legacy assets resources.

In config/application.rb:

config.assets.enabled = false

The following works (with an incorrect path):

resources :company_assets, :controller => 'assets', :as => :assets

But, of course, the path is '/company_assets/*'.

As soon as I try to make the path so it's '/assets/*', the routes fail to generate. By "fail to generate", I mean running:

rake routes | grep assets

Shows me nothing.

Here's an example of routes that fail to correctly generate:

resources :company_assets, :controller => 'assets', :as => :assets, :path => 'assets'

# or

resources :assets

When I poke around in the Rails console, it appears there are asset related routes, but they seem to be equivalent to having specified:

resource :assets

Instead of:

resources :assets

(Note singular vs. plural)

Is it possible to maintain backward compatibility here without mangling existing paths?

回答1:

Yes, it is possible.

The reason this happens is because Sprockets (which handles the asset pipeline) takes over the /assets route.

You can change the route that Sprockets uses by adding this to your application.rb:

config.assets.prefix     = "/x"

Where x is the new route.

I do this in one of my production apps because paperclip was storing its files in /assets.

One tip for upgrading: check the pipeline settings in your upgraded app against those in the last section of the asset pipeline guide.