Where to put partial data shared by the whole application in Rails?

Where can I place partial files shared by more than one model? I have a page called crop.html.erb that is used for one model - Photo . Now I would like to use it for another model called User .

I could copy and paste the code, but this is not very DRY, so I decided that I would move it to partial.

Since he shared between the two models - where would I place this partial?

Thank!

+45
ruby-on-rails dry partials
Mar 05
source share
6 answers

The Rails convention is to put common parts in /app/views/shared .

+56
Mar 05 '10 at 7:29
source share

Update

Layout inheritance is now found in the guides in layout and rendering. Template inheritance works the same way.

Rails 3.1 and future versions implement template inheritance, so I think the right place for shared partitions is now /app/views/application/ , let's say you are in products#index , you can do the following:

 -# products#index = render @products.presence || 'empty' -# /app/views/application/_empty.html.haml There are no items 

btw it application , because the connection is the inheritance of the controller, so this assumes ProductsController < ApplicationController

Thus, if you implement the /app/views/products/_empty.html.haml that will be executed, it will be rolled back for all missing particles, and I cannot check now, but I think even for the template itself ...

Railscast: template inheritance!

+45
Mar 27 '12 at 14:56
source share

TL DR

Rails 3.1, Rails 4, Rails 5 and all that comes next

 app/views/application 

The engine automatically searches for this path if the view is not found in the controller path.

Rails 3 and earlier

 app/views/shared 

The engine is NOT looking for this path automatically.




Long story

Rails 3 (and the previous version) have a default location for storing general views.

Informal agreement - keep general views in application / views / general . In any case, if you saved them, you must specify the path

 # render app/views/shared/menu.html.erb <%= render :partial => "shared/menu" %> 

This offer has been popularized by Agile Web Development with Rails .

Rails 3.1 introduces the official < official standard for storing general views:
app / views / app

Thanks to this standard, the engine now automatically searches for patterns in the application / views / application. As a result, you no longer need to use the full path .

Those curious can follow this decision here.

Old syntax

 # render app/views/application/menu.html.erb # unless menu.html.erb is found in appp/views/my_controller <%= render :partial => "menu" %> 

New syntax

 # render app/views/application/menu.html.erb # unless menu.html.erb is found in appp/views/my_controller <%= render partial: "menu" %> 

Of course, you can still post your general views wherever you want, and refer to them along the way

<%= render :partial => "my_own_special_shared_folder/menu" %>

If you have no reason to do so, please adhere to the new standard and keep the general views in app/views/application .

+13
Feb 07 '16 at 14:30
source share

Rails View uses app/views/layouts for shared parts such as headers and footers, but the Ruby on Rails Guide uses app/views/shared as an example . I think it comes down to personal preference. I would use layouts for higher-level materials such as the main navigator or footer, but shared for narrower controller-level elements such as form.

+4
Nov 27
source share

I have a shared folder in my views that contains commonly used parts.

+3
Mar 05 '10 at 5:12
source share

No matter where you put them. You can display any partial in any arbitrary place by specifying the path to the render file - it does not need to be connected to the controller that runs it. I use a directory, simply called partials in the view directory, and partially call in it as follows:

 render :partial => 'partials/mypartial' 
0
Mar 05
source share



All Articles