Routes behaviour inconsistent between server and local Rails with aliased resource - ruby-on-rails

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?

Related

User based custom subdomains

I've tried to research and figure out how people are doing this without much success.
For my rails project I have projects that each have their own slug. The user can themselves dictate the slug and that slug will be the subdomain.
Now I'm trying to figure out how to do this routing wise and make it work in production as-well.
I have this now:
get '/', to: 'posts#index', constraints: { subdomain: /.+/, via: [:get] }, as: :feed
At a controller level I do:
before_action :get_project, only: :index
...
def get_project
unless #project ||= Project.find_by_slug(request.subdomain)
redirect_to root_url
end
end
When I test this on localhost using lvh.me it works. However when I try to do this on a live production server and check server logs, Rails return this:
ActionController::RoutingError (No route matches [OPTIONS] "/"):
Does anybody have any experience with this?
As for why your current code doesn't work in production you'd need to give us your web server's configuration if we are to help you debug it since that is probably the main difference between the two environments. Especially if your app is served behind a reverse proxy. But you may be able to solve this using this gem. Their implementation of this covers several cases and simplifies what your trying to achieve and may end up working in production. It allows mapping of subdomain routes in several ways that really saves a lot of time. What you might be really interested in is this section that talks about mapping routes with active record.

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.

Rails::Engine routes not being loaded in production environment

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

Handling non-existent routes in Rails 3.1 application

I just switched to rails not long ago and I'm loving it. Everything works well in my rails 3.1 application, but now at the end I want to somehow handle routes like www.myapp.com/something (off course, I dont have something controller). I get a routing error when I visit this page, but I was wandering if there was a way to do something about it, even if it's only redirecting these routes to my root_url. I tried to find the answer online with no luck.
Yes, you can put a globbing route at the very end of your routes.rb to catch all misses:
match '/*paths', :to => 'some_controller#some_action'
in your Controller / action you can access the globbed path with
params[:paths]
more information http://guides.rubyonrails.org/routing.html#route-globbing
of course you can redirect without using an extra controller by using the redirect inline rack endpoint
match '/*paths' => redirect('/')

Why does '/' match correctly to a Rack application in my Rails 3 app, but no other route does?

I'm using Rails 3.0.6.
In my lib directory I have a example_app.rb, which is simply a Sinatra app:
class ExampleApp < Sinatra::Base
get '/' do
"Hello from Sinatra"
end
end
And I'm autoloading it with the application.rb config.auto_load_paths configuration.
In my routes file, I have only:
match "/" => ExampleApp
And that matches fine when I run the rails server (Webrick). However if I try:
match "/example" => ExampleApp
Visiting localhost:3000/example gives me a 'No route matches "/example"' error. Running rake routes shows the route though:
example /example(.:format) {:to=>ExampleApp}
If I try and match '/example' against a controller action it works fine, but not to that Sinatra app up above, so I'm not sure what's going on. I know there's something small I'm missing that I'm not finding in the routing documentation on the Rails site.
Thanks for any help.
The problem is that your Sinatra app only responds to requests made to /. You need to either add
get '/example'
or do a wildcard match using *
get '*' do
"Hello from Sinatra"
end

Resources