subdomain for static page in rails 3 - ruby-on-rails

I have a Rails 3 app that needs to have subdomains that point to a static page in my code base
EXAMPLE:
mk1.mysite.com needs to show the page that is located
app > views > about > page.html.haml
That page sits at mysite.com/about/page.html
QUESTION:
How can I configure the routes to display the subdirectory (mysite.com/about/page.html) page by visiting the subdomain (mk1.mysite.com)?
I have this in my routes.rb
match '/' => 'about/page.html', :constraints => { :subdomain => 'mk1' }

Where is your regular root route located in your routes file? Is it located before or after the line you listed in your post?
So if you have a line like
root :to => "foo#index"
In your routes file, above
match '/' => 'about/page.html', :constraints => { :subdomain => 'mk1' }
The root route would be called first and your subdomain route won't be called.

Related

Subdomain argument is ignored in my links, so all links are prepend with request.subdomain

I have a Ruby on Rails 4 website that I wish to split up in domain.com and mysubdomain.domain.com. I am using lvh.me for testing.
My routes file:
MyApp::Application.routes.draw do
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
get "first_page" => "pages#first_page", :as => :first_page
constraints subdomain: "mysubdomain" do
get "second_page" => "pages#second_page", :as => :second_page
end
root :to => 'pages#index'
end
end
In my view file I have:
= link_to "First page", first_page_path
= link_to "Second page", second_page_path(:subdomain => "mysubdomain")
But the subdomain argument is apparently ignored. Instead all links gets prepend with request.subdomain.
So if e.g. I am on:
http://mysubdomain.lvh.me:3000/second_page
Then the links on the webpage are as follows:
http://mysubdomain.lvh.me:3000/first_page # Not as intended
http://mysubdomain.lvh.me:3000/second_page # As intended
How to fix this?
The problem is you're using the _path helper and not the _url helper. The former generates a relative path so the site will use the current host where as the latter generates a full URL.
= link_to "Second page", second_page_url(subdomain: "mysubdomain")
should do what you're looking for.

Prefixed route path gives error in Rails 4

After upgrading to Rails 4 a route with a prefixed name and slash is throwing an error.
actionpack-4.0.1.rc1/lib/action_dispatch/routing/mapper.rb:239:in `default_controller_and_action':
'MyEngine/dashboard'
is not a supported controller name. This can lead to potential routing problems.
In routes.rb I have
Rails.application.routes.draw do
mount MyEngine::Engine => "/foo", :as => 'my_engine'
match 'dashboard' => 'MyEngine/dashboard', via: :get
And in the mounted engine MyEngine:
MyEngine::Engine.routes.draw do
match 'dashboard' => 'dashboard#index', via: :get
This works well in Rails 3.2, but in Rails 4 the slash in 'MyEngine/dashboard' throws the error.
Using an engine, you can directly create routes to the engine's controllers and actions in your routes file like this:
Rails.application.routes.draw do
mount MyEngine::Engine => "/foo", :as => 'my_engine'
get 'dashboard' => 'dashboard#index'
end
I think it's not possible to set a route in the host application to a controller in a mounted engine (mounted on "/foo") at the top level, e.g. /foo/dashboard calls the mounted engine's 'dashboard#index' action, and I want /dashboard to do the same.
I'm adding a controller of the same name in the host application and doing a redirect to the mounted engine controller action.
Simply, change this line
match 'dashboard' => 'dashboard#index', via: :get
as
get 'dashboard' => 'dashboard#index'

Link to Rails root URL when root is on a subdomain

So I have a Rails app with a static controller and two routes:
match '/', :to => "static#dashboard", :constraints => { :subdomain => "dashboard.alpha" }
root :to => "static#home"
The root is on alpha.mydomain.com and the second page is on dashboard.alpha.mydomain.com.
How can I link back to the root dynamically with Rails? The root_url variable is just / and using root_url(:subdomain => false) sends me to mydomain.com.
If you just want to get to alpha.mydomain.com and never to mydomain.com, you could make root_url always point to the alpha subdomain by doing this:
root :to => 'static#home', :subdomain => 'alpha'
And in the view you can just use:
<%= link_to 'home', root_url %>
Was that something like what you had in mind?

Rails: accessing network.domain.com renders root for domain.com

I'm trying to set up a subdomain network for my domain. In my config/routes.rb I have:
constraints :subdomain => /network.*/ do
...
# Root
match '/' => 'questions#index'
match '' => 'questions#index'
root :to => 'questions#index', :as => 'network_root'
end
root :to => 'frontend#home'
However, sometimes when I access network.domain.com (in production) it shows the root for the whole application (frontend#home). Accessing the subdomain in development works as expected.
EDIT: I've just noticed that requests to the app root (domain.com/) and subdomain root (network.domain.com/) do not get logged in my production.log. Any other page loads work as expected.

rails 3 routing not working as i would like

How would i go about routing the default page in my rails application to :
http://localhost:3000/pages/1
at the moment in my routes file i have:-
root :to => 'pages#show'
Just add the id param like:
root :to => 'pages#show', :id => 1

Resources