rails 3.2 link a subdomain to a controller? - ruby-on-rails

Update: rewritten question a bit. Trying to route my subdomains like below
login.app.ltd
user1.app.ltd
user2.app.ltd
signup.app.ltd
Using
Rails 3.2
Devise
To no avail tried several tutorials blog posts, anyone knows a working example for this?
Really stuck on this :(
this is my routes now:
match '', to: 'frontend#index', constraints: lambda { |r| r.subdomain.present? && ( r.subdomain != 'www') }
#match '' => 'home#index', :constraints => { :subdomain => 'login' }
constraints :subdomain => /^(?!signup\b)(\w+)/ do
root :to => "frontend#index"
end
root :to => "frontend#index"

My RailsApps project offers a complete example app showing how to use subdomains:
Rails Tutorial for Subdomains with Devise
Did you take a look at that?
config/routes.rb
devise_for :users
resources :users, :only => :show
constraints(Subdomain) do
match '/' => 'profiles#show'
end
root :to => "home#index"
lib/subdomain.rb
class Subdomain
def self.matches?(request)
case request.subdomain
when 'www', '', nil
false
else
true
end
end
end

Ok with some help managed to get it working
One should do:
constraints subdomain: 'login' do
devise_scope :user do
root to: 'sessions#new'
end
end

Related

Spina CMS routes set default landing page

I have Rails app that uses Spina CMS,
I want the landing page \ to be one of the pages in admin.
for ex. localhost:3000\home is my localhost:3000
currently getting 404 setting root :to => 'pages#home'
Seems root :to ... is getting overriden by Spina routes
routes.rb
Rails.application.routes.draw do
match '(*any)', to: redirect(subdomain: ''), via: :all, constraints: {subdomain: 'www'}
mount Spina::Engine => '/'
root :to => 'pages#home' # => not working...
get '/*id' => 'pages#show', as: "page", controller: 'pages', constraints: lambda { |request|
!(request.env['PATH_INFO'].starts_with?('/rails/') || request.env['PATH_INFO'].starts_with?("/#{Spina.config.backend_path}") || request.env['PATH_INFO'].starts_with?('/attachments/'))
}
end
In the route page replace root :to => 'pages#home' to root 'pages#home'
and add
resources :pages
Let me know if this works for you :)

Rails 4 static subdomain

I can't set root path for subdomain.
This is my routes.rb part:
constraints subdomain: 'blog' do
scope :module => "blog", :as => "blog" do
resources :posts
end
end
root 'statics#index'
When I am visiting blog.example.com I've got static#index action response and get posts#index, when visiting blog.example.com/posts.
I want to set root path for blog.example.com pointing to posts#index.
No effect for this:
match '/' => 'posts#index', :constraints => { :subdomain => 'blog' }, via: [:get]
I believe you should still be able to call root within the constraints block:
constraints subdomain: 'blog' do
root :to => 'posts#index' # Perhaps this needs to be `blog/posts#index`?
scope :module => "blog", :as => "blog" do
resources :posts
end
end
root 'statics#index'
This works for me with separate namespaces for each subdomain.

Overwrite root route in rails 4 for a certain subdomain

I'd like so overwrite my root :to => redirect("/projects") route if the subdomain is equal to "www" or is just "". This means if the subdomain is "www" or "" the app should use my website#index controller/action.
At the moment I got this setup:
class Subdomain
def self.match(r)
r.subdomain == "www" || r.subdomain == ""
end
end
.....
# website
scope :constraints => lambda { |request| Subdomain.match(request) } do
get '/' => 'website#index'
get "/help" => "website#help", as: "help"
get "/about" => "website#about", as: "about"
get "/signup" => "website#signup", as: "signup"
# post "/signup" => "website#signup_account"
end
root :to => redirect("/projects")
If I access www.satisfy.dev/help or satisfy.dev/help everything works fine. If I access www.satisfy.dev/ or satisfy.dev/ the root_path (/projects) is in use. I thought the get '/' => 'website#index' should be more important than the root_path since it's above the root_path.
Hope somebody has a hint for me!
I tried treating the routes as "get" requests and had more success.
See here:
Multiple 'root to' routes in rails 4.0

Rails: Define routes manually after modifying to_param

I have been trying to implement a vanity url for user profiles, based on the example here: Rails 3: Permalink public profile. I have replaced the 'id' with username:
def to_param
username
end
However this has caused issues with my other routes. I have set them so that they match the default sets of routes exactly, when running 'rake routes'.
get '/users/' => 'users#index', :as => :users
post '/users' => 'users#create'
get '/users/new' => 'users#new', :as => :new_user
get '/users/:id/edit' => 'users#edit', :as => :edit_user
patch '/users/:id' => 'users#update'
put '/users/:id' => 'users#update'
delete '/users/:id' => 'users#destroy'
# for vanity url
get '/:id' => 'users#show', :as => :user
With this setup, trying to access delete and update routes give me 'no route matches' error. What is the proper way to specify these, and / or should I be doing this a different way? Any help is appreciated.
I think it's interresting and more readable to keep the resources syntax in the routes.rb, except for the show, which you can rewrite to customize user_path :
resources :users, :except => [:show]
# 2 possibilities for the show url
get '/users/:id' => 'users#show' # can be removed if you don't want to keep /users/:id url
get '/:id' => 'users#show', :as => :user
But change the controller to find user by username instead of id, for example
def show
#post = Post.find_by_username(params[:id]) # instead of Post.find(params[:id])
# ...
end

Include subdomain for constrained route in Rails url helper

Say I have the following routes that are constrained to specific subdomains:
App::Application.routes.draw do
constraints :subdomain => "admin" do
scope :module => "backend", :as => "backend" do
resources :signups
root :to => "signups#index"
end
end
constraints :subdomain => "www" do
resources :main
root :to => "main#landing"
end
end
My problem is that root_url and backend_root_url both returns a url on the current subdomain: "http://current-subdomain.lvh.me/" instead the subdomain specific for the resource.
I would like root_url to return "http://www.lvh.me/" and backend_root_url to return "http://admin.lvh.me/" (the behavior should be the same for all resources under the subdomain).
I have tried to accomplish this in rails 3.2 by setting the url options in various places, one being url_options in application controller:
class ApplicationController < ActionController::Base
def url_options
{host: "lvh.me", only_path: false}.merge(super)
end
end
Maybe I need to override the url helpers manually? How would I approach that (accessing the routes etc)?
Edit: I'm able to get the correct result using root_url(:subdomain => "admin") which returns "http://admin.lvh.me/" regardless of the current subdomain. However, I would prefer not having to specify this all over the code.
Using "defaults" as shown below will make rails url helpers output the correct subdomain.
App::Application.routes.draw do
constraints :subdomain => "admin" do
scope :module => "backend", :as => "backend" do
defaults :subdomain => "admin" do
resources :signups
root :to => "signups#index", :subdomain => "admin"
end
end
end
constraints :subdomain => "www" do
defaults :subdomain => "www" do
resources :main
root :to => "main#landing"
end
end
end

Resources