Heroku/Rails route example.com to mydomain.example/example - ruby-on-rails-3

My rails app is a way for users to create their own basic website. So each of them will have a root folder that is example.com/user/1. They will have other pages like example.com/user/1/about etc. How do I configure Rails and Heroku so that something like www.user1.example points to example.com/user/1, and something like www.user1.example/about directs to example.com/user/1/about?

The way I did in my app was first have the list of user domains in the database. Then create an advanced constraint
http://edgeguides.rubyonrails.org/routing.html#advanced-constraints
In this case, my constraint class looks like this:
class UserDomainConstraint
def self.matches?(request)
UserDomain.all.map(&:domain_name).include? request.domain
end
end
And then use that constraint in the route
root to: 'user_domains#index', :constraints: UserDomainConstraint
My action then takes a look at request.domain and acts accordingly.

Related

what is '*' in ASP.NET MVC 4 Routes

What does the * in the line:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
mean?
Does it mean "match everything that looks like: {resource}.axd/1/2/3/4/5 or something like that?
The wildcard provides a catch-all route. It allows, as you assume, any number of paramters after the wildcard parameter:
AnyResource.axd/any/number/of/parameters/will/be/valid
It's also useful when creating a CMS and you want process the url yourself rather than using static routing parameters. Example:
"{*slug}"
You could create a lookup table in your database and retrieve the specific page for the provided slug.

MVC4 construct url with controller/parameter/action

have a look to the following URL:
https://manage.windowsazure.com/#MYCOMPANY.onmicrosoft.com#Workspaces/CloudServicesExtension/CloudService/MYSERVICENAME/dashboard
It looks like the url is in the form {controler}/ActionName/{parameter}/ActionName
How can I generate such URL in MVC4/Razor?
Indeed, I want to build url in the form:
http://www.myservice.com/Configuration/{Source or Target}/{DomainName}/{ConfigurationType}
ex:
http://www.myservice.com/Configuration/Source/mydomain.com/ConnexionConfiguration
http://www.myservice.com/Configuration/Source/mydomain.com/AttributesConfiguration
Thanks.
You can either create per action routes, for each action, or include the controller name and action name in the url and then use something like this:
routes.MapRoute("Default","{controller}/{source}/{domain}/{action}")
so a controller called Configuration with an action called Setup and a domain of example.com and source of welcome would be
/Configuration/welcome/example.com/setup

Rails 3 Routes - prepend all url paths with set string

I've been asked to change the routes on a Rails project such that the routes will only respond to requests where the app name (or other arbitrary string) is the first string after the domain name, e.g.
www.thething.com/appname/users/sign_in instead of www.thething.com/users/sign_in
www.thething.com/appname instead of www.thething.com
www.thething.com/appname/search instead of www.thething.com/search
I've suggested using a subdomain appname.thething.com instead, but the client is quite specific about wanting the URL in the above format.
www.thething.com will be a splash page which will contain a link to www.thething.com/appname, with the intention of adding additional apps/pages in future with new folder names.
Is there an easy way of modifying the routes file so that all routes will get the .../appname prepended to all resources and routes, while being after the domain?
One option is wrap all existing routes in: namespace :appname do ... end, like so:
# config/routes.rb
Appname::Application.routes.draw do
namespace :appname do
# existing routes go here
end
end
I'm not sure if this is the most elegant solution, but it will prepend /appname to all the routes.

Make URL be title of post

Currently my URL's appear as www.website.com/entries/1, I'd like to make them appear as www.website.com/title-of-entry. I've been messing around with routes and have been able to get the entry title to display in the URL, but Rails is unable to find the entry without supplying an ID. If I send the ID along with the parameters, the URL appears as www.website.com/title-of-entry?=1. Is there anyway I can pass the ID without having it appear in the URL as a parameter? Thanks!
Like most things, there's a gem for this.
FriendlyID.
Installation is easy and you'll be up and running in minutes. Give it a whirl.
Ususally you'll want to to save this part in the database title-of-entry (call the field slug or something`). Your model could look something like this:
class Entry < ActiveRecord::Base
before_validation :set_slug
def set_slug
self.slug = self.title.parameterize
end
def to_param
self.slug
end
end
Now your generated routes look like this: /entries/title-of-entry
To find the corresponding entries you'll have to change your controller:
# instad of this
#entry = Entry.find(params[:id]
# use this
#entry = Entry.find_by_slug(params[:id])
Update
A few things to bear in mind:
You'll have to make sure that slug is unique, otherwise Entry.find_by_slug(params[:id]) will always return the first entry with this slug it encounters.
Entry.find_by_slug(params[:id]) will not raise a ActiveRecord::RecordNotFound exception, but instead just return nil. Consider using Entry.find_by_slug!(params[:id]).
If you really want your routes to look like this /title-of-entry, you'll probably run into problems later on. The router might get you unexpected results if a entry slug looks the same as another controller's name.

Multiple Resource Routing In Rails

I have two unrelated models, say Person and Building. When the app receives a url like www.mysite.com/JohnDoe/EmpireState I would like to show properties of the Person with the name johnDoe, and the same for the building with the name EmpireState.
I'm confused as to the routing part specifically. I'm unsure if I need to create a pages controller that can return the objects from the database. How should I go about doing this?
Am hoping for something like below?
match ':user_name/:building_name', :controller => pages
If those two are not related, you shouldn't do it that way. If they ARE related, we call that nested resources.
Example:
resources :projecs do
resources :tasks
end
Sample URL: "/projects/12/tasks/1281"
Edit:
If they are NOT related (taken from my comment):
In your BuildingsController you can fetch the parent informations too. If you use the match route in your question, you'll have params[:user_name] AND params[:building_name] available and can fetch anything you want with them...
Building.find_by_name(params[:building_name]) # return all Buildings based on URL param