Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a view path to Rails's partial rendering lookup?

Tags:

I'd like to have the following directory structure:

views/   app1/     users/_user.html.erb     users/index.html.erb    app2/     users/index.html.erb    shared/     users/_user.html.erb     users/index.html.erb 

In my view, I'd call

# app1/users/index.html <%= render :partial => "user" %> # => /app1/users/_user.html.erb   # app2/users/index.html <%= render :partial => "user" %> # => /shared/users/_user.html.erb 

So basically, how do I tell Rails to check in the /app2/users dir then the shared dir before it raises it's missing template error?

Update


I got around this (as suggested by Senthil, using File.exist?

Here's my solution - feedback and suggestions welcome

# application_helper.rb  # Checks for a partial in views/[vertical] before checking in views/shared def partial_or_default(path_name, options={}, &block)   path_components         = path_name.split("/")   file_name               = path_components.pop   vertical_file_path      = File.join(vertical}, path_components, file_name)   shared_file_path        = File.join("shared", path_components, file_name)   full_vertical_file_path = File.join("#{Rails.root}/app/views/", "_#{vertical_file_path}.html.erb")   attempt_file_path       = File.exist?(full_vertical_file_path) ? vertical_file_path : shared_file_path   render({:partial => attempt_file_path}.merge(options), &block) end 
like image 858
bodacious Avatar asked May 21 '11 12:05

bodacious


People also ask

How do you render partials?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What is partial view in Rails?

Ruby on Rails Views Partials Partials allow you to extract pieces of code from your templates to separate files and also reuse them throughout your templates. To create a partial, create a new file that begins with an underscore: _form.html.erb.

How can you tell Rails to render without a layout *?

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.

What is render in Ruby on Rails?

Rendering is the ultimate goal of your Ruby on Rails application. You render a view, usually . html. erb files, which contain a mix of HMTL & Ruby code. A view is what the user sees.


1 Answers

There's already something built into rails that facilitates this type of "theming" for you. It's called prepend_view_path.

http://api.rubyonrails.org/classes/ActionView/ViewPaths/ClassMethods.html#method-i-prepend_view_path

There's also append_view_path for adding paths to the end of the lookup stack.

I have this successfully working in production:

 class ApplicationController < ActionController::Base    before_filter :prepend_view_paths     def prepend_view_paths      prepend_view_path "app/views/#{current_app_code}"    end  end 

Now every controller will first look in "views/app1" (or whatever your dynamic name turns out to be) for the views corresponding to the action being called.

It's also smart enough to check all the defined paths for the file you're looking for, so it rolls back to the default location if one isn't found.

like image 182
twmills Avatar answered Sep 18 '22 12:09

twmills