How to process requests for certain sub-domain(s) in Rails? - ruby-on-rails

For example, I want my project link to look like http://blog.example.com. How can I make that kind of route in Rails 4? And how it can interact with controller?
The following stuff didn't work for me.
resource :article, constraints: {subdomain: 'blog'}

Your code is acting as a type of filter, so only requests already utilizing the subdomain 'blog' will invoke the route. You need to ensure that the web server itself is also setup to handle that subdomain and point it to your application.
Edit: Try checking out this post for further clarification (if you're on your local environment). https://reinteractive.net/posts/199-developing-and-testing-rails-applications-with-subdomains

Related

How do I hardcode a route root for Url.Action when generating a confirmation link

My Asp.net core web app can generate a confirmation link successfully using
string confirmationLink = Url.Action("SetPassword","Account",
new
{
userid = userMaster.Id,
token = confirmationToken
},
protocol: HttpContext.Request.Scheme);
This roots the route using the root of the current controller's page.
Now I have moved out the logic to a web api and I would like to use the root of the calling page in the Url.Action statement in the webapi.
I want to achieve following points.
a) get the root of the current page and
b) having passed that root to the webApi how do I seed Url with it so that it is available to Url.Action?
Workaround: I am generating the confirmation link as a simple string.
string confirmationLink = ${this.config.p_ConfirmationURLRoot}/{this.config.p_ConfirmatonURLController}/{this.config.p_ConfirmationURLAction}?userId={ userMaster.Id}&token={ confirmationToken}";
This does not resolve the questions posed above, but if like me your end goal is a working link that may be used else where, this approach will satisfy that requirement.
It sounds like you're trying to work with request-routing for a front-end application using the routing of a separate API. This isn't a use-case for the ASP.NET routing systems; they are only interested in routes within the application domain. The front-end application has its routes and the API has its routes: both are completely separate concerns.
This is a good model, one you want to work with rather than against. The front-end application should be free to change its URLs without requiring changes in other applications.
By all means, have a separate API to perform logic like user-creation and validation of security tokens, but these are not concerns which should be intermingled with front-end routing.

Can I build part of my website in Ruby on Rails?

I have an existing website written in jsp. I want to rewrite a part of the website. The url for that section can either be newpart.mysite.com or mysite.com/newpart.
Will it be possible to rewrite this new part in Ruby on Rails? How does the routing works for both the url options.
Yes, you can use ROR for a portion of your site.
If your using a webhost they will route the domain to a folder on your server.
The way you worded your question, it seems as if your "newpart" will be a separate interface than the rest of the code for your site. You can act as if they are on different servers basically.
Yes you can do that.
For your options:
If you point the whole subdomain at rails: newpart.mysite.com, then all you have to do is tell apache (or whatever you use) to redirect that URL to your RoR app - and the app will happily continue from there without any changes to routing.
It would be more complex if you wanted to use: mysite.com/newpart ... so if you have a choice, I'd go with the subdomain.

Any relation between emberjs route and rails route

One project based on emberjs and rails.
When redirect to localhost/#lessons/2, the page works
when redirect to localhost/practices/2#/lessons/2,
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
so what's the difference between 'localhost/#lessons/2' and 'localhost/practices/2#/lessons/2'
If want to make 'localhost/practices/2#/lessons/2' works, how to set emberjs route?
so what's the difference between 'localhost/#lessons/2' and 'localhost/practices/2#/lessons/2'
Difference is the /practices/2 part. That's part of the url's path. By default ember will ignore that, it's just paying attention to the hash, which in bot cases is lessons/2.
Like firefox says, seems like the server is redirecting the request.
If want to make 'localhost/practices/2#/lessons/2' works, how to set emberjs route?
Hmmm... that url implies that server/rails is responsible for rendering practices/2 and that you have an ember app on the practices/2 page which should be rendering lessons/2? It's possible but that sounds like a very complicated setup. I'd be surprised if that's what you really want. Probably instead you will want to have localhost/#practices/2/lessons/2. With that setup just use normal ember routing as described here: http://emberjs.com/guides/routing/defining-your-routes/

Should admin be a subdomain or a namespace?

When would you use a subdomain over a namespace? i.e. http://admin.foo.com VS http://foo.com/admin
Alternatively, I also like how api.foo.com looks VS foo.com/api. I also find, subdomains a bit tricky to set up.
Mounting another app inside a folder or a subdomain is no big deal with Web-Servers, but if your Rails app contains both the /admin and normal applications it gets trickier to serve one as a subdomain.
Thankfully the Rails router is very flexible in this regard and supports both scenarios rather well.
TLDR: Rails supports both ways through the routing engine and at this point it comes down to personal preference (although I suspect the subdomain option will not play too nicely with path helpers)
/admin Routes
To achieve the /admin routes, Rails supports the notion of namespaces in routing. So having a /admin area in the Rails app you just write this in your routes.rb like this:
namespace :admin do
resources :users
resources :posts
end
You then put the controllers for the /admin area in controllers/admin/.rb and the class has to be prefixed with Admin (like Admin::PostsController).
Since most application's Admin area will most likely interact with the Models from the normal application it's probably safe to say doing namespacing is the most convenient way.
Subdomain Routes
But namespacing can also be used with subdomains as it turns out:
The Rails router can define constraint blocks and define the namespace inside these blocks.
So if you want to host the namespace from above only in the admin.example.com subdomain you can do this:
constraints(:subdomain => /admin/) do
namespace :admin do
resources :users
resources :posts
end
end
(I didn't know about the contraints feature but this blog post seems to explain it quite well)
This obviously requires you to configure the web server in a way that it serves admin.example.com and www.example.com to the same Rails application.
I am not sure if session (achieved through cookies) is carried over but I guess you can figure this out.
I think the other answer addressed the practicality issue, but purely from a security perspective:
Putting admin in a subdomain is recommended in the Rails Security Guide because it is more insulated from an XSS attack:
Put the admin interface to a special sub-domain such as
admin.application.com and make it a separate application with its own
user management. This makes stealing an admin cookie from the usual
domain, www.application.com, impossible. This is because of the same
origin policy in your browser: An injected (XSS) script on
www.application.com may not read the cookie for admin.application.com
and vice-versa.
So from a security perspective, putting admin in a subdomain may be safer.

Rails 3 custom domains for users on Heroku

I'm looking to set up custom domains for users. Much like Tumblr does.
I understand that the user must point their A record to an IP address. I found some information here: Custom domains in a Rails App
Can someone give me an example of this with a Heroku/Rails 3 setup? Is it even possible?
If you setup a wildcard DNS to point to your main app, you can use the :subdomain in config/routes.rb to handle your business logic.

Resources