RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12224

How can I route my pages so that I am serving up slightly different content by subdomain in Rails 3.2?

I have a web application that is going to be used for two large companies. The app consists of a main static "company website" and the app itself. Only the static site needs to be separate, and of the 10 pages only 3 of them need to be company-specific. So I have this:

app/views/info (static area)
...about.html.erb
...company1_faq.html.erb
...company2_faq.html.erb
...company1_index.html.erb
...company2_index.html.erb
...company1_pricing.html.erb
...company2_pricing.html.erb
...privacy.html.erb
...license.html.erb
...terms.html.erb

I want to split these out by subdomain, then redirect based on subdomain for just these actions. However, I would like to strip the subdomain out of the URL also, which I don't know how to do. Can anyone help?

Upvotes: 0

Views: 32

Answers (2)

RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12224

Railscasts is always my first stop so I did follow Ryan's directions ;-)

Since I only need it for one controller, this is the rest of what I did and it's working great:

At the top of info_controller.rb:

before_filter :redirect_to_subdomain_page, only: [:index, :about, :pricing, :faq]

Then at the bottom:

private

def redirect_to_subdomain_page
  render "#{request.subdomain}_#{params[:action]}"
end

Perfect! It preserves the regular URL also.

Upvotes: 1

Yeggeps
Yeggeps

Reputation: 2105

You can access the subdomain through the Request object

Like so:

request.subdomain

You could then put the partials in different folders

app/views/info/company1
app/views/info/company2

And then render them based on the subdomain.

<%= render "info/#{@subdomain}/faq" %>

Upvotes: 1

Related Questions