Xavier Frenette
2005-Jun-20 20:12 UTC
different views and behavior for same action and controller
Hi all, I have 3 subdomains: www.dn.com admin.dn.com members.dn.com I know how to detect the subdomain, so it''s not my question (http://wiki.rubyonrails.com/rails/show/HowToUseSubdomainsAsAccountKeys) I want *.dn.com/categories/new to behave differently depending on the subdomain: www.dn.com/categories/new -> I want a 404 admin.dn.com/categories/new -> a certain view is displayed members.dn.com/categories/new -> another view is displayed Of course the controller action "/categories/create" will behave differently depending on the subdomain. My question is: What is the best way to achieve this (after I stored the subdomain in a variable)? - add a "if subdomain == ''admin'' then ... ; elsif subdomain =''members'' then ..." to the controller''s actions and to the category''s views? - specify rails a different folder to look for the controllers and the views depending on the subdomain (how?) - Have 3 different deployments sharing the same db (and the same Model???) - other... Thank you very much Xavier
Tyler Kiley
2005-Jun-20 20:38 UTC
Re: different views and behavior for same action and controller
I''m currently working on something similar (different controllers/views for different domains). I haven''t tested it, but it seems like you could organize your controllers into modules (admin, members, etc) and then use apache url rewriting to prefix the controller path with your subdomain, so members.dn.com/categories/new becomes members.dn.com/members/categories/new That solution reminds me of the good old days when MSIE didn''t support name-based virtual hosting, though. It just isn''t very pretty :p Tyler On 6/20/05, Xavier Frenette <xavierfrenette-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I have 3 subdomains: > www.dn.com > admin.dn.com > members.dn.com > > I know how to detect the subdomain, so it''s not my question > (http://wiki.rubyonrails.com/rails/show/HowToUseSubdomainsAsAccountKeys) > > I want *.dn.com/categories/new to behave differently depending on the subdomain: > www.dn.com/categories/new -> I want a 404 > admin.dn.com/categories/new -> a certain view is displayed > members.dn.com/categories/new -> another view is displayed > > Of course the controller action "/categories/create" will behave > differently depending on the subdomain. > > My question is: > > What is the best way to achieve this (after I stored the subdomain in > a variable)? > - add a "if subdomain == ''admin'' then ... ; elsif subdomain => ''members'' then ..." to the controller''s actions and to the category''s > views? > - specify rails a different folder to look for the controllers and > the views depending on the subdomain (how?) > - Have 3 different deployments sharing the same db (and the same Model???) > - other... > > Thank you very much > Xavier > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Matt Jankowski
2005-Jun-22 16:17 UTC
Re: different views and behavior for same action and controller
Did you get any useful responses to this? I face a similar problem in an app I''m building - and the same issue also comes up with different views for logged in vs. not logged in, etc. It seems like doing a bunch of if/else logic is very dirty, and there must be a simpler way. For now, I''ve resorted to just NOT re-using the same controller for two different things, but that forces me to have lines like if subdomain == "members" redirect_to :controller => ''home'' to accomodate everywhere people might end up... -Matt Xavier Frenette wrote:> Hi all, > > I have 3 subdomains: > www.dn.com > admin.dn.com > members.dn.com > > I know how to detect the subdomain, so it''s not my question > (http://wiki.rubyonrails.com/rails/show/HowToUseSubdomainsAsAccountKeys) > > I want *.dn.com/categories/new to behave differently depending on the subdomain: > www.dn.com/categories/new -> I want a 404 > admin.dn.com/categories/new -> a certain view is displayed > members.dn.com/categories/new -> another view is displayed > > Of course the controller action "/categories/create" will behave > differently depending on the subdomain. > > My question is: > > What is the best way to achieve this (after I stored the subdomain in > a variable)? > - add a "if subdomain == ''admin'' then ... ; elsif subdomain => ''members'' then ..." to the controller''s actions and to the category''s > views? > - specify rails a different folder to look for the controllers and > the views depending on the subdomain (how?) > - Have 3 different deployments sharing the same db (and the same Model???) > - other... > > Thank you very much > Xavier > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Xavier Frenette
2005-Jun-22 22:21 UTC
Re: different views and behavior for same action and controller
This is the solution I came up with: 1) I group my controllers into "modules" (explanations below) 2) Since I use subdomains to differentiate the "user type" (I have admin.sd.com, members.sd.com and www.sd.com (the default)), I use url_rewrite to redirect to the correct "module" and controller (as in #1) as suggested by Tyler. Explanations: 1) Controllers can be grouped in modules, so you can have 2 different "categories" controllers: www.dn.com/members/categories/show/12 www.dn.com/admin/categories/show/12 In this situation, you have a Categories controller in the members module and a Categories controller in the admin module. (2 different files with different (or same) actions that can do different things) The best way to create them and learn how they work is to use the controller generator: ruby script/generate controller members/Categories (where "members" is your module) (for help: ruby script/generate controller -h or http://wiki.rubyonrails.com/rails/show/ControllerGenerator) This will create in your controllers/ directory a folder named "members" which will contain the categories_controller.rb file. Open it to see that the class declaration will be a little bit different (for a newbie like me). Use the generator since it will also create the "members" directory in the "views" folder and also in the test folder. The default route map (:controller/:action/:id) will recognize your module so you don''t have to create new routes. 2) About the subdomains: Instead of www.dn.com/admin/categories/show/12, I wanted admin.dn.com/categories/show/12. I didn''t do it yet, but, as suggested by Tyler, I will probably use url_rewrite to redirect admin.dn.com/controller/action/id to www.dn.com/admin/controller/action/id Hope it works :) Xavier 2005/6/22, Matt Jankowski <mjankowski-3V1LzDqIiU+4+7hldlPAjkEOCMrvLtNR@public.gmane.org>:> Did you get any useful responses to this? > > I face a similar problem in an app I''m building - and the same issue > also comes up with different views for logged in vs. not logged in, etc. > > It seems like doing a bunch of if/else logic is very dirty, and there > must be a simpler way. > > For now, I''ve resorted to just NOT re-using the same controller for two > different things, but that forces me to have lines like > > if subdomain == "members" redirect_to :controller => ''home'' > > to accomodate everywhere people might end up... > > -Matt > > > Xavier Frenette wrote: > > Hi all, > > > > I have 3 subdomains: > > www.dn.com > > admin.dn.com > > members.dn.com > > > > I know how to detect the subdomain, so it''s not my question > > (http://wiki.rubyonrails.com/rails/show/HowToUseSubdomainsAsAccountKeys) > > > > I want *.dn.com/categories/new to behave differently depending on the subdomain: > > www.dn.com/categories/new -> I want a 404 > > admin.dn.com/categories/new -> a certain view is displayed > > members.dn.com/categories/new -> another view is displayed > > > > Of course the controller action "/categories/create" will behave > > differently depending on the subdomain. > > > > My question is: > > > > What is the best way to achieve this (after I stored the subdomain in > > a variable)? > > - add a "if subdomain == ''admin'' then ... ; elsif subdomain => > ''members'' then ..." to the controller''s actions and to the category''s > > views? > > - specify rails a different folder to look for the controllers and > > the views depending on the subdomain (how?) > > - Have 3 different deployments sharing the same db (and the same Model???) > > - other... > > > > Thank you very much > > Xavier > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails >
Duane Johnson
2005-Jun-24 03:23 UTC
Re: different views and behavior for same action and controller
On Jun 22, 2005, at 10:17 AM, Matt Jankowski wrote:> Did you get any useful responses to this? > > I face a similar problem in an app I''m building - and the same > issue also comes up with different views for logged in vs. not > logged in, etc. > > It seems like doing a bunch of if/else logic is very dirty, and > there must be a simpler way. > > For now, I''ve resorted to just NOT re-using the same controller for > two different things, but that forces me to have lines like > > if subdomain == "members" redirect_to :controller => ''home'' > > to accomodate everywhere people might end up... > > -MattI thought I''d post this idea in case it is helpful. We also have an admin/member/public type setup for our application, and our main layout file (application.rhtml) is used for all types of users because there is a lot of common html across all user types. So instead of duplicating much of the layout, we used the following helper method to include partials such as the "status bar" and the "menu bar" wherever they should appear in the layout: def render_partial_for_user_type(partial_name) if @session[''user''].nil? # Neither member nor admin is logged in return render_partial("layouts/public/" + partial_name) elsif @session[''user''].member? # Member is logged in return render_partial("layouts/member/" + partial_name) elsif @session[''user''].admin? # Admin is logged in return render_partial("layouts/admin/" + partial_name) else return "" logger.warning "Rendering layout for an unknown user type: user is not public, member or admin." end end Then, a simple call like this will render the correct partial: <%= render_partial_for_user_type(''menu_bar'') %> Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails