Custom URLs in Rails of the form custom.site.com - ruby-on-rails

I would like to have my users specify custom URL paths such that those paths are placed in front of my site's name, i.e. if I have a site called www.orion.com, I'd like a user to be able to create his own little home page at johnny.orion.com.
I have successfully managed to implement orion.com/johnny, which works implemented by adding map.connect ':path' at the end of my routes then making sure the path variable is present when needed.
How can I get johnny.orion.com to function?

First, you'll need to set up wildcard DNS - so that the subdomains actually resolve to somewhere.
Then, configure your virtual host to accept connections from these subdomains:
# If you're using Apache, something like:
ServerAlias *.orion.com
Then, you can use the subdomain-fu gem to handle your routes within Rails. Have a look at the associated Railscast for some good tips.
The syntax for the gem is something like this:
link_to 'Posts', post_path(#post, :subdomain => 'johnny')
johnny.orion.com/posts/4

Related

Rails - Add a route prefix to a specific directory

I have a messy rails 3 application that's come from a different developer and I need to refactor it.
What I want to do is move the contents of "app" into a subfolder called "classic".
app/classic
And then have all URL's with a classic prefix such as
localhost:3000/classic/wills/new
Route to controllers inside of the "app/classic" folder.
And then every regular url that does not contain the classic prefix - route in the standard way to app/
Is it possible to do this? The only thing I've discovered so far is that I can add a scope inside of my routes file.
scope(:path => '/classic')
But all that does is require a prefix for every URL. I'm really not sure how to go about this!
This is a route namespace. Take a look at this section in Rails Routing from the Outside In: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
namespace :classic do
# your routes here
end
This will do 3 things -
the path to the controller files need to be under /app/controllers/classic/
the name of the controllers need to change to Classic::ControllerName
the url is now /classic/controller/action
This sounds like what you want, but you can modify this to get just the parts you want if you don't want all 3.
In route.rb file:
#Of course, you have to configure the right http method.
get 'wills/new' => 'wills#new', as: 'to_classic_wills_new'
Hope this helps!

Rails URL and Path Helpers in Engines Like Spree

I'm trying to use some URL and/or path helpers in my Rails 4 Engine views such as resource_url or resource_path. These engines are configured a bit differently than the typical --mountable tutorial out there. In fact, they more closely resemble how Spree does it, without the Spree::ENGINE_NAME namespace.
Like Spree's architecture, I'm attempting to create core engine that my other engines depend on. For example, my backend engine's routes.rb file looks like this:
Core::Engine.add_routes do
# Check to see if the request comes in on a subdomain
# The Subdomains class passed into constraints()
# is a class defined in lib/subdomain.rb
constraints(Subdomain) do
match '/manage' => "manage#index", :via => [:get]
end
end
In a view inside my backend engine, I'd like to be able to use some URL/path helpers to do something like this:
<%= link_to manage_path, manage_path %>
This doesn't work, because I'm drawing the routes on the core engine. So, I must use
<%= link_to core_engine.manage_path, core_engine.manage_path %>
Spree somehow gets around this, but I'm not sure how. For example, in backend/app/views/spree/admin/products/index.html.erb:
<%= link_to product.try(:name), edit_admin_product_path(product) %>
Notice, the edit_admin_product_path, but no mention of this actually being drawn on the core engine.
Any ideas?
We get around this by drawing all the routes on the core engine using add_routes which exists for reasons I won't go into here because it's a long tangent. Necessary evil for this kind of work, though.
The isolate_namespace method within Core::Engine scopes everything to the spree namespace. If you're inside a controller that's been drawn underneath the Spree::Core::Engine routes and you want to reference a route for another controller also drawn under that route then you can leave off the spree. prefix on the routing helper.
If you're routing to a different engine, then you will need to have the prefix: main_app. or whatever.
The Engines Guide explains this in greater detail, and I'd recommend reading that.

Prepend path prefix to all rails routes

I have a setup where nginx serves a rails application inside a specific subfolder
eg. http://myserver/railsapp/ and everything inside gets proxied to rails, if the first subfolder is different, it servers static files from another folder.
I haven't been able to find how to specify this behaviour in rails in an intelligent way. I mean, what I want is to specify an option like Rails.server_prefix = /railsapp so that all the routes get prepended automagically, both on the incoming requests and on the generated links.
You probably want to use the router's scope method with the :path argument:
Rails.application.routes do
scope(:path => '/railsapp') do
# the rest of your routes go here
end
end
See the docs for more info.

How do I write a full path in a controller in Rails 3?

I need to write the full path so need to know what the rails_root domain is. How do I do that? For example:
string = "{RAILS_ROOT}/vendors/#{#vendor.id}"
What is the equivalent of "RAILS_ROOT" to give me what the full domain is for my application? So that in development it would subsstitute localhost:3000 and on my heroku site the right full domain?
You should always avoid, if possible, hard-coding your path, because it is less flexible and more prone to result in broken links in the future. Plus, you can use Rails routing, which is an elegant way to generate everything cohesively in Rails without any need to create the composite parts yourself.
If you have your routes set up properly, you should be able to call:
link_to "View vendor", vendor_url(#vendor.id)
Vendor_url(#vendor.id) in Rails gives you your full URL, which you can then contain in your string variable. Here's how to generate the routes needed for the above:
# in routes.rb
resources :vendors
Try:
File.realpath(RAILS_ROOT)
You could access the request object. request.host_with_port would give you the hostname and port. request.protocol will give you the protocol (http:// or https://). request.fullpath will give you the path with query params.

github url style

I wanted to have users within my website to have their own URL like http://mysite.com/username (similar to GitHub, e.g. my account is http:// github. com/sr3d). This would help with SEO since every profile is under the same domain, as apposed to the sub-domain approach.
My site is running on Rails and Nginx/Passenger. Currently I have a solution using a bunch of rewrite in the nginx.conf file, and hard-coded controller names (with namespace support as well). I can share include the nginx.conf here if you guys want to take a look.
I wanted to know if there's a better way of making the URL pretty like that.
(If you suggest a better place to post this question then please let me know)
Cheers,
Alex
Place this line right at the end of the routes.rb file, ( So that this doesn't interfere with other controller routes )
map.connect "/:username", :controller=> "users", :action => "show"
in users_controller, use the following line to fetch the user
#user = User.find_by_username(params[:username])
I don't think this would require any nginx magic or url rewrites.
HTH

Resources