Cant define root route more than once in Rails 4 - ruby-on-rails

I've got the following in my routes file:
scope :constraints => lambda{ |req| req.session[:user_id].present? } do
root "users#show"
end
scope :constraints => lambda{ |req| req.session[:admin_id].present? } do
root "brands#index"
end
root "sessions#new"
This code worked fine in Rails 3, but when I use it in Rails 4 I get the following error message:
Invalid route name, already in use 'root' (ArgumentError).
You may have defined two routes with the same name using the ':as' option
Is there a way round this? What has changed?

As #vimsha pointed out, it's a known issue and in my case the best fix was to do the following:
scope :constraints => lambda{ |req| req.session[:user_id].present? } do
match '/', to: "users#index", via: :get
end
scope :constraints => lambda{ |req| req.session[:admin_id].present? } do
match '/', to: "brands#index", via: :get
end
root "sessions#new"
Alles im ordinem.

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.

Dealing with multiple root paths and scopes in Rails

We have the following routes setup:
MyApp::Application.routes.draw do
scope "/:locale" do
...other routes
root :to => 'home#index'
end
root :to => 'application#detect_language'
end
Which gives us this:
root /:locale(.:format) home#index
root / application#detect_language
which is fine.
However, when we want to generate a route with the locale we hitting trouble:
root_path generates / which is correct.
root_path(:locale => :en) generates /?locale=en which is undesirable - we want /en
So, question is, is this possible and how so?
root method is used by default to define the top level / route.
So, you are defining the same route twice, causing the second definition to override the first!
Here is the definition of root method:
def root(options = {})
options = { :to => options } if options.is_a?(String)
match '/', { :as => :root, :via => :get }.merge(options)
end
It is clear that it uses :root as the named route.
If you want to use the root method just override the needed params.
E.g.
scope "/:locale" do
...other routes
root :to => 'home#index', :as => :root_with_locale
end
root :to => 'application#detect_language'
and call this as:
root_with_locale_path(:locale => :en)
So, this is not a bug!

rails 3.2 link a subdomain to a controller?

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

How do you make the root_url one level above the root/ and still keep the routes intact

I have a working application with proper routes.
The application then became shared with another application with a homepage below the two sites.
-- Root Homepage --
/ \
| |
Website 1 Website 2
Now when I go to my page, the site loads. But the functioning does not.
My Routes
match '/generate_csv/(:id)', :to => "main#generate_csv", :via => :post, :as => "generate_csv"
resources :main
root :to => 'main#new'
Currently, my application is prepended by rails/website_1
How can I get my routes to line up again?
UPDATE
So this resolves the #create method, but I am still trying to get that first match route working..
scope "rails" do
resources :main
match 'main/generate_csv/(:id)', :to => "main#generate_csv", :via => :post, :as => "generate_csv"
end
The problem is solved by writing the routes twice. This can't be right:
scope "rails" do
resources :main
match 'main/generate_csv/(:id)', :to => "main#generate_csv", :via => :post, :as => "generate_csv"
end
resources :main
match 'main/generate_csv/(:id)', :to => "main#generate_csv", :via => :post, :as => "generate_csv"
root :to => 'main#new'
It might be easier to use a subdomain as an alternative when merging two separate sites by doing something like:
constraints(:subdomain => /website1/) do
match '/' => 'website2/dashboard#index'
end
Otherwise, maybe try a scope:
scope module => "website1" do
resources :main
end

Resources