Rails 3.1 force .html instead of no extension - ruby-on-rails-3

One of my clients wants his new Rails application to look more like his traditional web site. He wants to know if I can force urls to have a file extension, preferably .html.
I don't want to hard-code the extension in routes.rb as
match ':controller/:action/:id.html'
(or similar) because the client also wants to have a respond_to-style JSON API which requires the use of .:format.
Can this be done?

Just as Mattias Wadman suggested, in config/application.rb add:
AppName::Application.default_url_options = { :format => "html" }
But also change config/routes.rb to:
root :to => 'pages#home', :defaults => { :format => "html" }

Im no Rails routing expert but I tried to force HTML format by changing the default URL options and at least the URL helpers seams to generate .html URLs now, it's a start.
config/application.rb (at the bottom)
AppName::Application.default_url_options = {:format => "html"}

Related

Subdomain routing with devise not working on heroku

I have defined a few types of users using devise (members, company_users, etc) and I'd like to use different subdomains for the login pages of each type of user.
I've referred to this railscast in order to implement the matching of the subdomain and redirect to the appropriate action. My routes.rb file looks like this:
devise_for :company_users, :controllers => { :registrations => 'company_users/registrations', :sessions => 'company_users/sessions' }
devise_scope :company_user do
constraints Subdomain do
match '/' => 'company_users/sessions#new'
end
end
And my lib/subdomain.rb file:
class Subdomain
def self.matches?(request)
request.subdomain.present? and request.subdomain =~ /\Acompanies\z/
end
end
Locally, it works perfectly. I've tested using companies.lvh.me:3000 (as the same railscast suggests) and it really redirects to the correct login page.
In order to try and make it work on Heroku I have added the domain, using heroku domains:add companies.mydomain.com, and I have added a new CNAME record on my DNS server, pointing to my Heroku application.
However, when I try to access companies.mydomain.com it redirects me to the root path, and not to the correct login page. I'm kind of clueless of what's happening. Any help will be appreciated.
This happen when the tld of your domain is different from tld of the heroku domain.
Mine is .com.br and I have to add config.action_dispatch.tld_length = 2 to production.rb, so Rails can parse the URL correctly and redirect to the right subdomain.

Install drupal in a directory beside an existing website

I want to Install a drupal website on the server in a subdirectory to use some of it's features for my main website, I dont want the installation affect the current website in any way.
the actual reason for doing this is that for example I would be able to use news section of drupal for main website.
so for instance, I install drupal on "drupal" subdirectory like www.mydomain.com/drupal , then when I configure and run the news section it will be like www.mydomain.com/drupal/news, what I want is when a user goes to www.mydomain.com/news, it loads www.mydomain.com/drupal/news instead.
I'd really appreciate any proper approach and suggestions to achieve this.
If I correctly understand your question that you want to have URLs like:
http://example.com/news/headline-test
But using Drupal's URL http://example.com/drupal/news/headline-test to provide the content of the first URL, you should use Apache rewrites.
Here is an example:
Add this to your top .htaccess folder (http://example.com/.htaccess - not publicly accessible yes):
RewriteEngine on
RewriteRule ^news/(.*) drupal/news/$1
See the Apache rewrite document for more examples/help.
Drupal cannot take over any folder if there is another file exists or it's pointing to another folder. But in this case, both of your http://example.com/drupal/news/headline-test and http://example.com/news/headline-test URLs will show the same content.
Do not edit Drupal's $base_url setting. Let Drupal figure it out. Also, add some robots.txt disallows to prevent content duplication. Block drupal's native URLs.
Take a look at redirect module.
OR
Create a custom module with hook_menu implementation
function MY_MODULE_menu()
{
$items = array();
$items['news'] = array(
'title' => 'news redirect',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'page callback' => 'drupal_goto',
'page arguments' => array('drupal/news'),
);
return $items;
}

Subdomain constraints and url helpers on a domain with a subdomain (sub-subdomain?)

So I have read all about how to use Rails 3 subdomain constraints and url helpers, which works great for most applications. For instance, if I want an admin subdomain (which I do) I can use:
constraint :subdomain => :admin
scope :module => :admin
defaults :subdomain => 'admin'
But my application runs on many (customers') domains, and usually on a subdomain of it. So, something like directory.customer.com. Now if I apply that logic to admin.directory.customer.com, Rails things directory is part of the subdomain, so I have to do this:
constraint :subdomain => /^admin.*/
scope :module => :admin
defaults :subdomain => 'admin'
This is great, just match any subdomain that stats with admin, BUT when it comes to using the url helpers, it's NOT great, because the default subdomain I've set (admin) doesn't include the customer's portion of the subdomain (directory).
Setting the :host option to directory.customer.com doesn't seem to fix this, the url helper still returns admin.customer.com. Is there any way to set it up so url helpers "know" that the directory.customer.com is all part of the :host and the subdomain shouldn't overwrite that part of the host name? There's got to be a method I can modify or something to make it keep that host name intact, right?

Change default route name

In my Rails 3.1 application i have a UsersController, which gives me /users URL.
But i need /u instead for all REST actions.
What is the best practice for that?
You need to define the "path", in your case, modify routes.rb to
resources :users, :path => 'u'

creating a launch page in rails app

I am planning to create an app in rails but first I want to make a launch page. Having never made a launch page I am curious as to how others are doing it?
Do you creae a small rails application with controller and model that just collects email addresses? and then deploy the rails app? I'd prefer this way but it seems like an overkill to deploy a rails app just for a launch page...?
Also, how do you modify the routes file so that if users type anything after the url then only page that shows up is the laungh page.
Meaning, if my launch page is at http://mycoollaunchpage.com then if users mess around and type http://mycoollaunchpage.com/lkjlkjljk then it should redirect back to http://mycoollaunchpage.com
Your idea sounds good. Just a page with an email signup form would work well.
To redirect back to your home page, make a route glob in your routes.rb file, and have an action in your controller that just redirects back to your root.
# in routes.rb
match "*whatever", :controller => 'pages', :action => 'redirect_to_root'
# in your pages_controller.rb file
def redirect_to_root
redirect_to "/"
end
There is an awesome rails plugin available for this very requirement of yours ;)
https://github.com/vinsol/Launching-Soon/