Routing specs and subdomain constraint - ruby-on-rails-3

I have a route which is constrained to only run when a subdomain is present, which looks something like this:
# Set the default root for when a subdomain is used.
match '', to: 'public::pages#show', constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'app' }
# Set the default application route.
root :to => "pages#index"
I'm looking to put together some routing specs to test that this routes properly (testing the app manually shows that the route does in fact work just fine) however I'm coming up against a few issues.
My current shot at a routing test for this is simply to supply the entire domain, with subdomain as part of the get() call:
it "routes to public page on subdomain" do
get('http://somesubdomain.example.com/').should route_to("public::pages#show")
end
However this spec fails, where the router has directed to the root event, rather than the intended one.
<{"controller"=>"public::pages", "action"=>"show"}> expected but was
<{"controller"=>"pages", "action"=>"index"}>.
Can anyone suggest why this might not be working?

Sounds like your root :to => "pages#index" route is taking precedence, and the match ''... route is getting ignored. I would try two root routes and set your :constraint lambda on the first one:
# Set the default root for when a subdomain is used.
root to: 'public::pages#show',
constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'app' }
# Set the default application route.
root :to => "pages#index"
See this blog post for more details about using constraints on your root route.

Related

Select SSL Routes serving up rails 4 static pages via highvoltage gem

I have several static erb pages being served up in a ruby rails 4 site via the high voltage gem:
get '/about' => 'high_voltage/pages#show', id: 'about'
get '/contact' => 'high_voltage/pages#show', id: 'contact', :protocol => "https"
get '/privacy' => 'high_voltage/pages#show', id: 'privacy'
This all works well and good, except that the /contact route doesn't redirect or force SSL on, it is happy with whatever protocol is used.
I host the site on engine yard, attempting to put :force_ssl only or variants in the route line resulted in failed deployments - high voltage uses a slightly different set of arguments than normal routes so I suspect there is a conflict somewhere.
Anyone use highvoltage and SSL with rails 4 for select static pages (not the whole site)? Example routes line please.
You can achieve this by overriding the HighVoltage#PagesController see the override section of the documentation.
It might look something like this:
class PagesController < ApplicationController
include HighVoltage::StaticPage
before_filter :ensure_secure_page
private
def ensure_secure_page
if params[:id] == 'contact'
# check to make sure SSL is being use. Redirect to secure page if not.
end
end
end
Next disable the routes that HighVoltage provides:
# config/initializers/high_voltage.rb
HighVoltage.routes = false
Then in your application's routes file you'll need to set up a new route:
# config/routes.rb
get "/pages/*id" => 'pages#show', as: :page, format: false

Subdomain constraint (Rails 3) makes local server (thin) SO SLOW

I recently added a subdomain constraint to my Rails routes file
constraints(:subdomain => 'new') do
devise_for :customers do
get "/customers/sign_up" => "registrations#new"
post "/customers" => "registrations#create"
put "/customers/:id" => "registrations#update"
end
match '/' => 'roxy#index'
namespace :roxy, :path => '/' do
resources :customers
resources :surveys
end
end
In order to test the subdomain routing constraint locally, I added this line to my hosts file.
127.0.0.1 new.localhost.local
Now, I test my app in my browser at the URL new.localhost.local:3000. It takes about 10 - 15 seconds to load every page, which is unreasonably slow. If I remove the subdomain constraint and just go to 127.0.0.1:3000, everything is zippy and fast again.
What am I doing wrong? I'm new to Rails, so please tell me if there is a better way to do subdomain routing in rails, or if there is a setting I need to configure.
Figured it out. It's nothing to do with Rails or subdomains or thin. Turns out, unlike other unixy-things, OS X reserves the .local TLD for mDNS functionality. For every page, the DNS resolution was timing out before loading my app. So I just changed my /etc/hosts file to
127.0.0.1 new.localhost.dev
and everything's working great now.
Read more: http://www.justincarmony.com/blog/2011/07/27/mac-os-x-lion-etc-hosts-bugs-and-dns-resolution/

Redirecting subdomain to folder in Rails 3

I need to redirect help.mydomain.com/a-page to /pages/a-page without using Nginx or Apache redirects and only through Rails 3 routes.
The key here is the help subdomain determining that /pages/ should be dropped from the route although the view is under the /views/pages/
Any help is appreciated.
Try this:
match "/:page_name" => redirect("/pages/%{page_name}"), :constraints => { :subdomain => /help/ }

Default root url and display the controller action rather than "/" in rails 3

Is there a way to set the default root routes in rails to be one of my controller action and showing in the url rather than the root "/" ?
Say I have a "Computer" controller with a "index" action.
When my user login to my application, I want the url to be
http://localhost:3000/computer/index rather than http://localhost:3000/
root :to => "computers#index"
does the latter one, how can I make the default root url to be something like the prior one ?
UPDATE: a better way would be
root :to => redirect(/path)
The simplest way would be to change which URL your users get re-directed to after they successfully log in.
You could also add a forced re-direct in your controller:
# routes.rb
root :to => "computers#force_redirect"
# computers_controller.rb
def force_redirect
redirect_to '/computers/index'
end

Routes how to hide them on the url bar rails 3

My route file has the following in it for the root. what i want is when i click on say about it doesn't show me http://127.0.0.1:3000/home/about what i would like is either http://127.0.0.1:3000/about
or just the URL http://127.0.0.1:3000/ like root dose. I'm running rails 3 if you can post a snip it of a route file that will do this or give me a url to see it at that would be great.
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => "home#index"
# See how all your routes lay out with "rake routes"
well, if you have a controller home with an action about and want to call about directly, you can do something in your routes like match "/about" => "home#about" :as => :about
if you want to call the route, for example by using a link_to you can say it like this:
<%= link_to "Home", about_url %>
you can do rake:routes to see all the generated routes, you'll see one mapped to about with controller => home and action => about
Then, when you call http://localhost/about it will actually call the home controller's about action
you can read more material about routing here
Check out http://edgeguides.rubyonrails.org/routing.html for more details.
You should put the root route at the end of the file.
You need to delete the public/index.html file for the root route to take effect.
Make sure you have a home controller
Make sure you have a view called index in the views/home/ folder
Then http://127.0.0.1:3000/ will display your index action
If you want your root to go to the about action, also change the route file to root :to => "home#about". Of course make sure you have an about view.