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)
Related
I've made authorization following this tutorial: https://www.railstutorial.org/book/sign_in_out#cha-sign_in_sign_out but now I want to add subdomains to my application.
I've added this to my routes.rb:
match '/' => 'students/board', :constraints => { :subdomain => 'student' }, via: 'get'
If I want to redirect user after sign in to his subdomain like this:
redirect_to :subdomain => 'student', :path => '/'
I'm getting this error:
No route matches [GET] "/sessions"
If I redirect user without a subdomain he is normally redirected. I don't understand why it's trying to get 'sessions' path. I would be grateful for some suggestions. I didn't find anything online which is related to login sessions and subdomains.
Thanks!
There might be something in your code you aren't showing us redirecting you to the sessions path.
Have you tried naming the path then redirecting there?
routes.rb:
get '/' => 'students/board', :constraints => { :subdomain => 'student' }, as: 'student_login'
then use:
redirect_to student_login_path
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
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?
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 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.