On Feb 21, 6:58 pm, Owain
<owain.mcgu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I have an app that varies its content based upon the domain from which
> it is being accessed. Some of the domain characteristics are
> supported in the model but it is easier varying static text in the
> views and then sharing the form templates via partials etc.
>
> Rails 2.3.10 and looking at the documentation
athttp://rubydoc.info/docs/rails/2.3.8/ActionController/Base#prepend_vi...
>
> application_controller.rb
>
> before_filter :domain_lookup, :set_view_paths
>
> protected
>
> def set_view_paths
> self.prepend_view_path "app/views/#{controller_name}/
> #...@domain.policy.policy_type}"
> end
>
> according to the documentation this should add this path to the front
> of the search order for this request only (as opposed to the similar
> class method which prepends for all future requests)
>
> I would expect the template to be rendered for the controller from app/
> views/quote/type1/new.html.erb if it exists, if it does not exist it
> will fall back to app/views/quote/new.html.erb
Tada. Spending a bit of time in debugger and going through the Rails
code here is where it all happens:
http://rubydoc.info/docs/rails/2.3.8/ActionView/PathSet#find_template-instance_method
So it goes hunting for load_path + template_path on line 60 where
load_path is iterated over view_path that I set in the application
controller and template path is "controller/action"
In an nutshell the view path is just the set of "roots" from which the
template_path (i.e. controller/action) is loaded as the template.
All I needed to do is change my directory structure for this:
application_controller.rb
before_filter :domain_lookup, :set_view_paths
protected
def set_view_paths
self.prepend_view_path ["app/views/variants/
#{@domain.policy.policy_type}"]
end
and set out my view directories as follows:
app/views/controller_name becomes the fall-back or default set of
views if the variant of the view does not exist
app/views/variants/policy_type/controller_name is where you put all of
the variants of the view for the specific variant.
This approach does have a nice side-effct of allowing all of the
references to partials from within the view of also being loaded in
the same fashion. For example, if I want a variant of a form for one
of the domains, all I need to do is put the modified version of the
form partial in the appropriate directory and all the relative paths
to partials can remain the same. As they say. Neat.
All in all this is a nice technique for using the same application to
serve multiple variants (skins) based on domain or sub-domain name.
I did find similar postings trying to do the same thing. Either I and
they misread the documentation or it is not very clear how view_paths
evaluate to the actual filename. I''ll look into putting in an
amendment to the documentation.
O.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.