Rails::Engine routes not being loaded in production environment - ruby-on-rails

I'm having a really strange problem whereby my routes are working fine in development mode but not in production.
The majority of my app is contained within an engine, which is loaded into the main application, and the routes file the main app starts like this...
SandersteadParish::Application.routes.draw do
mount Cms::Engine => "/cms"
The first few line of my routes file in the engine look like this...
Cms::Engine.routes.draw do
scope '(groups/:group_id)' do
resources :pages do
resources :widgets
collection do
post :layout, :add_panel, :split_panel
end
end
If I run RAILS_ENV=development rails server, the routes work fine, but if I run RAILS_ENV=production, they don't get loaded.

when you add add engine is "isolated" by default this means all it routes needs to be accessed with the engine scope for example Cms.pages_path you can find more info here http://edgeguides.rubyonrails.org/engines.html#routes

Related

How's it possible that we can invoke resources and get methods inside routes.rb without error

Lets say I have a rails routes.rb file
Rails.application.routes.draw do
resources :articles
end
I guess Rails has made the necessary inclusions(via "require" or "include") so that whenever routes.rb is invoked by itself or from another ruby file(either in the rails console environment or from the rails server which we can run via the "rails server" command), it functions well and in a error-free way. These inclusions made it possible that we can call the
Rails.application.routes.draw
method inside the routes.rb file.(So Rails has made Rails.application.routes available for routes.rb.)
But how is it possible that we can call the resources method inside routes.rb? If resources is a class method of some class A or a method of some module B (which as I noted, is possible for Rails to make available to routes.rb), then the only way resources could have been invoked inside routes.rb - would be, to use
A.resources
or,
B::resources
(correspondingly), right?
This also applies to other methods we use inside routes.rb, such as
get

ember/rails - Strange behavior in rails route containing an underscore

I'm playing around with a tester app using an ember frontend and using jsonapi-resources to build the rails api. My routes in the rails app are defined as follows
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
jsonapi_resources :books
jsonapi_resources :authors
jsonapi_resources :publishing_houses
end
However I've noticed something strange. If I go to the path for publishing_houses like so: http://localhost:3000/publishing_houses it says the route/page doesn't exist. However if I go to http://localhost:3000/publishing-houses with a dash instead of an underscore between publishing houses, I get the response that I want.
The problem is in my ember app, I've checked the console and it requests data using this url: http://localhost:3000/publishing_houses with the underscore, so I don't get any data back in my ember request.
Is there a reason for this behavior? Or am I doing something wrong?
I did some digging and there appears to be a config for the route format. Try this
# config/initializers/jsonapi.rb
JSONAPI.configure do |config|
config.route_format = :underscored_route
end
That should turn the routes into "/publishing_houses" instead of "publishing-houses" which would make them compatible with the other library you are using.

Insert routes programmatically in Rails

I'm creating an engine that needs to insert some routes into the application's router. For this particular gem, I'd rather not application's routes.rb if possible. Is there a way to insert routes at a particular location in the router via code? I'm looking for an API that does something like:
Rails.application.routes.insert("resources :foos", :before => "some string in routes.rb")
If I create a config/routes.rb inside the engine and define some routes, this kind of works. Rails is smart enough to mix the engine's routes into the application's routes, but it tacks them on at the end of the route list. I need them to appear at the beginning so the engine's routes take priority.
I'm aware that I can namespace the routes by mounting the engine in the application's routes.rb, but this creates a routing structure that I don't really want. I want the engine's routes to look they are a part of the application by defining some routes in the actual application.
I have a workaround which is to add the following to the application's routes.rb.
Rails.application.routes.draw do
MyEngine.setup_routes(self)
#...other routes below
end
MyEngine.setup_routes looks like
def self.setup_routes(map)
map.get 'a_path', :to => 'a_controller#a_path'
end
This at least allows me to control the point where the routes get defined in the application's route list, but the user still has to manually update his routes.rb (or I have to build an installer that does it). It seems like there should be a way to tell rails to tack some routes onto the start of the route list...

Routes behaviour inconsistent between server and local Rails with aliased resource

I have a rails app using Rails 2.2.2 and ruby 1.8.6 (i know i should update, don't go there).
I have this entry in my config/routes.rb file:
map.namespace :music_service_admin do |music_service_admin|
music_service_admin.resources :users, :as => :teachers
end
Which gives me urls like
/music_service_admin/teachers
/music_service_admin/teachers/123
etc
I just had a bug where i had accidentally used /music_service_admin/users in an ajax call. I missed the bug because on my local machine, in development OR production mode, both these routes work: I can use ALL of the /music_service_admin/teachers routes with "/users" instead of "/teachers", and they behave the same.
On our server, however, it will only accept the /music_service_admin/teachers routes - the /music_service_admin/users urls aren't recognised and therefore 404.
In both instances (server and local), if i look at rake routes it only has routes for the /music_service_admin/teachers urls: it doesn't have any for /music_service_admin/users. So, according to rake routes, i'd expect /music_service_admin/users to NOT work on my machine. So i guess it's my local rails that's anomalous, and the server is behaving as expected.
Any ideas why it might be different?

Routes in Engine mounted on subdomain do not inherit the constraints

Setup
Our current Rails app is made out of sub-apps that are mounted as engines. Typically these engines are mounted on a subdomain in the main routes.rb file as follows
mount MySubApp::Engine => '/', as: :sub_app, constraints: {subdomain: 'sub_app'}
The Problem
Routes within MySubApp's routes.rb file do not get the subdomain when using the named _url helpers. For example the following in apps/my_sub_app/config/routes.rb
MySubApp::Engine.routes.draw do
resources :foos
end
gives us sub_app.foo_url(5) but it results in
http://www.example.com/foos/5
when we want
http://sub_app.example.com/foos/5
tl;dr
How can I get the engine's mounting constraints passed to its named routes?
EDIT: A Workaround
While I'd still prefer a better solution, the following will work. You can wrap all the routes in each of the sub apps routes.rb files that could be mounted on a subdomain like so
MySubApp::Engine.routes.draw do
constraints Rails.application.routes.named_routes[:sub_app].constraints do
resources :foos
end
end
EDIT 2: A much less desirable workaround
A commenter (since deleted?) pointed out you can pass a subdomain option to the helpers but we'd like to avoid having to use sub_app.foo_url(5, {subdomain: 'sub_app'}) for every cross subdomain link. Even if we moved the subdomain name into an ENV var and made a wrapper, this is not DRY.
check out the guide it says you can do it by
namespace :Engine do
resources :controller, :methods
end
the Engine is just name spacing your code
#Aaron not sure if you ever got this fixed, but look into the
config.action_dispatch.tld_length
setting (on the engine's config). I'm not sure how it'll react with engines, but in our case it lets us handle the case of sub-subdomains for our staging server (so when we use the _url helpers with the staging server it correctly does subdomain.staging.domain.com, rather than subdomain.domain.com).

Resources