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?
Related
I'm looking for a way to use subdomains as a permalink in rails.
For example:
Instead of http://domain.com/user2 pointing to user2's profile.
I would like http://user2.domain.com to point to user2's profile.
Anyone know of a solution to this that will work on heroku?
Routes:
get '/' => 'home#index', :constraints => { :subdomain => 'www' }
get '/' => 'users#show', :constraints => { :subdomain => /.+/ }
Users Controller:
#user = User.find_by_permalink(request.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
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!
How would I stop these 2 routes from clashing:
match "users/:id/edit", :to => "users#edit", :via => :get, :as => :settings
match '/:username', :controller => 'users', :action => 'show'
When I visit localhost:3000/settings it tries to find a user in my users table with the username "settings".
What I plan on doing is having a page settings then have things like settings/privacy, settings/general and also edit_profile
How can I have this but also still get to user localhost:3000/username
It must be possible as I've seen many ROR sites doing this.
Kind Regards
The settings route you have here is pointing to the users edit path.
Using settings_path will direct to users/:id/edit
What you want is:
match "/settings", :to => "users#settings"
match "/settings/:category", :to => "users#settings"
match '/:username', :controller => 'users', :action => 'show'
Now '/settings' will direct you to the settings method. The second additional route will handle the specific setting to route to (ie /settings/privacy) which will allow you to evaluate what to do in the view based on the params[:category] parameter available in your settings action. Alternatively this will give a privacy settings path:
match "/settings", :to => "users#settings"
match "/settings/privacy", :to => "users#settings", :as => :privacy_settings
match '/:username', :controller => 'users', :action => 'show'
No need for the :as statement, settings_path will still work. Then in the settings method of your UsersController you can use session information(current_user) to render the proper user.
The RESTful edit path will be handled and available through this statement in your routes file:
resources :users
So 'users/:id/edit' will still work.
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