0

I am using Rails version 2.3.14 and I am trying to figure out the best way to use a different layout for an entirely new section of my site but I can't think of the most efficient way to do it.

e.g. I have a CRM and everything is localhost/controller/method as normal but what I want to do is create a sub-section of my site named portal.

I considered creating a new app but I don't really want to do that since this sub section will be using existing controllers so I don't want to end up in the situation of having to remember to update both controllers to make sure their contents are identical as that will end up getting messy.

I read this answer rails 3, how add a view that does not use same layout as rest of app? and it makes sense but I can't think how to apply that to my situation.

e.g. http://localhost/ would show my existing login page and something like localhost/portal would show the login page for the new section but with a completely different layout but using the same models and controllers.

I'm new to rails so I might be over-thinking it or I might be barking up the wrong tree.

If it's possible I think the easiest way would be to somehow check the url for portal and set the layout based on the outcome of that check.

Community
  • 1
  • 1
martincarlin87
  • 10,848
  • 24
  • 98
  • 145

1 Answers1

1

I read the answer you posted, it makes sense, I just try to give you some real codes in your case.

You have a sub site, it means that your path of all pages in sub site will start with '/portal', right?

In your app/views/layout , you can add a layout named "portal_subsite.html.erb".

In your application_controller.rb

class ApplicationController < ActionController::Base
  layout :get_layout

  private

  def get_layout
    if  request.path.start_with?('/portal')
      'portal_subsite'
    else
      'application'
    end
  end
end

I have not a environment of Rails 2 now, but I think it should work, if you have any questions, please comment below.

Bigxiang
  • 6,252
  • 3
  • 22
  • 20
  • thanks @Bigxiang, my existing url works (`localhost`) but if I go to `locahost/portal` I get the `Unknown Action` error but that makes sense since it thinks `portal` is a controller. Is there a way to tell the app to ignore the `portal` part and process requests as normal? – martincarlin87 Aug 30 '13 at 08:56
  • what do you want `locahost/portal` to go? Have you set the correct route in route.rb? – Bigxiang Aug 30 '13 at 08:58
  • I'm looking for a way to use existing controllers and models with a different layout. So both pages would do the same thing but look different. e.g. the `portal` section will use some existing controllers but the existing app will not use anything from the `portal` section. Hope that makes sense. I haven't set up the route yet, I will try it just now. – martincarlin87 Aug 30 '13 at 09:01
  • infact, I have just realised I don't know how to set up this route, e.g. something like `map.connect 'portal/:action/', :controller => ':action'` but that's not valid. – martincarlin87 Aug 30 '13 at 09:04
  • try `map.connect 'portal/:controller/:action/:id'` ? you can access such as "portal/users/login". This method just solves a part of problems you have, some routes need to be defined twice I think. – Bigxiang Aug 30 '13 at 09:11
  • hmmm, still `Unknown Action` – martincarlin87 Aug 30 '13 at 09:14
  • What's your path you passed in the browser? – Bigxiang Aug 30 '13 at 09:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36520/discussion-between-bigxiang-and-martincarlin87) – Bigxiang Aug 30 '13 at 09:19