Ruby On Rails Subdomain Convention - ruby-on-rails

I have a domain, domain.com, and I want one Rails application, for example, to handle all subdomain requests, i.e., blog.domain.com, subdomain.domain.com, etc.
The Ruby on Rails framework is all about convention. So, is there a convention for creating subdomains in Rails? If so, what is it? If not, what might be some good methods for trying to get what I described above accomplished?

You could follow this tutorial on using subdomains in Rails:
http://railsapps.github.io/tutorial-rails-subdomains.html
Basically, you implement routing for subdomains like this:
match '/' => 'profiles#show', :constraints => { :subdomain => /.+/ }
Url helper methods also accepts a subdomain option so you can write url helpers like:
link_to root_url(:subdomain => user.name), root_url(:subdomain => user.name)
You'll find more details as to how to use subdomains in the link provided above.

Related

How does routes of Ruby On Rails know which controller to hit when we hit an url

How ROR routes recognise which controller method to hit while we hit an url.
It's a way to redirect incoming requests to controllers and actions. It replaces the mod_rewrite rules. Best of all, Rails' Routing works with any web server. Routes are defined in app/config/routes.rb.
Think of creating routes as drawing a map for your requests. The map tells them where to go based on some predefined pattern.
The routes.rb file defines the actions available in the applications and the type of action such as get, post, and patch.
like:
get 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
the normalise value of above route is.
app: #<ActionDispatch::Routing::RouteSet::Dispatcher:0x007fd05e0cf7e8
#defaults={:format=>"jpg", :controller=>"photos", :action=>"show"},
#glob_param=nil,
#controller_class_names=#<ThreadSafe::Cache:0x007fd05e0cf7c0
#backend={},
#default_proc=nil>>
conditions: {:path_info=>"/photos/:id(.:format)", :required_defaults=>[:controller, :action], :request_method=>["GET"]}
requirements: {}
defaults: {:format=>"jpg", :controller=>"photos", :action=>"show"}
as: nil
anchor: true
The official Ruby on Rails documentation explains this question in a thorough and understandable way:
http://guides.rubyonrails.org/routing.html

Subdomain instead of resource-plural as url base for controller/routing logic

Sorry if my title isn't clear enough.
In my app I have a blogs resource. Now, normally this could turn out like "www.example.com/blogs/new", "www.example.com/blogs/the-internet-blog" etc. but what I want is to move the "blogs" from behind the .com to being a subdomain. ("blogs.example.com/new", "blogs.example.com/the-internet-blog").
Do I now really need to route all my actions basically manually or is there an option for a rest resource to make it aware of that tiny url logic change?
Yes, you can specify a subdomain in your Rails routes, using a :subdomain constraint as described in the Rails Routing Guide:
constraints :subdomain => "admin" do
resources :photos
end
And to construct links using subdomains, you can use the url_for extension described in this ASCIICast post:
Subdomains in Rails 3
Based on Stuart M's answer I got it to work now, I also was missing the important path-option.
These are my routes now for the blog logic:
constraints :subdomain => "blogs" do
resources :blogs, :path => "/"
end

Rails Routes - Prevent accessing same controller#action through different urls?

I've noticed that when defining routes in the routes.rb file, you can actually access the same controller#action you defined in a different way.
For example:
map.connect "post/show/:id/:tag_title", :controller => "post", :action => "show", :requirements => {:id => /\d+/}
This means you can access post#show by going to
server.com/post/show/1234/tag_title-whatever
But you can also access post#show by going to
server.com/post/show?id=1234&tag_title=tag_title-whatever
It's a simple case, but you get the idea. Wouldn't this cause problems with search engines? If I'm not wrong, those 2 urls could potentially be taken as duped pages.
Is there a way to prevent this, like telling Rails to access the defined routes only the way they're defined?
Being able to access something doesn't mean that it will be indexed by Google. All Rails helper functions use the /post/show/.. links unless told differently. As long as you don't link to the specified resource, the likelihood of Google spidering the content is very slim.
That being said, I'm not even sure it would matter if they did end up indexing it.

What is the best way to deal with Vanity URL helpers in Rails 3?

I have a web application I am working on with Rails 3 and I have just implemented some basic Vanity URL paths to existing resources in the application. What I am looking to do is to not have to explictly build the urls on the user's profile page for the resources that are available, e.g. I would like to be able to build a URL with link_to in the view in the format of:
typealoud.com/:user_id/:thread_id/:comment_id
And not what the standard nested resource helpers give me, something like:
typealoud.com/threads/:thread_id/comments/:comment_id
Should I do this myself as a URL helper, or is there an existing gem?
To do this, I would put this at the top of my routes:
match ':user_id/:thread_id/:id', :to => "comments#show"
I've changed comment_id in this example to id because it's "The Rails Way" that the last id parameter is simply called id. It also results in shorter code.
If you wish to have a routing helper for it use the :as option:
match ':user_id/:thread_id/:id', :to => "comments#show", :as => "comment"
Then you can use comment_path/comment_urlto access the route, but you must pass in three arguments to it, each of them being an object or an id of an object.

Modifying rails route helper

I'd like to modify the behavior of the rails route helper *_url for a single route/page.
Here's what I'm try to do:
User visits:
http://test1.myapp.com/account
All the *_url routing helpers resolve to http://test1.myapp.com/ as normal.
But, then if the user goes to https://myapp.heroku.com/account/billing?id=test1
I'd like all the *_url routing helpers on that page to resolve to: http://test1.myapp.com/
instead of http://myapp.heroku.com/
So, is it possible to change the domain bit for all the *_url helper calls for a specific page?
For those interested, I'm trying to use heroku's piggyback ssl method for my app for just a secured billing page.
You can actually just modify the links that point to the billing area:
<%= link_to "Billing", my_helper_url(test1, :host => "myapp.heroku.com", :protocol => "https") %>

Resources