Just learning rails and looking for best practices help. I have the same data table that is reused across several different views (index, search results) for my controller. In an attempt to keep it DRY I have the table code in a helper method using html<< for table HTML.
I realize that now I've pulled a chunk of my HTML into the controller which I'm not a big fan of. How is this situation of having a chunk of HTML you plan to reuse across several views handled best?
Thanks!
What you want are partials. You put the partials in the same folder as the views, but partials start with an underscore (e.g: app/views/user/_my_partial.html.erb). In this partial you can put the shared HTML code, and access it from the view with:
<%= render "my_partial" %>
Yes you skip the underscore when you access the partial.
See the rails guide for more information on partials.
Related
I have partials created for several of my models. However, I also have a search controller that I would like to use different partials for. The reason for this is that the search controller displays a collection of various model types, so the partials I want to use in that view also display the model name.
I'm currently rendering with this line in views/search/index.hmtl.erb: <%= render #results %>.
Is there a way I can easily tell the call to render using the partials contained in the views/search directory? Or should I do this differently altogether?
I am pretty new to Rails. One of the requirement is to have a Filter Panel that would appear along each record list and will be used to filter records based on criteria. What actually I am looking for an HTML based UI of it that will contain input fields along with labels that I would like to pass from Controller. Since I will be using it across views so I don't want to put in a view specific folder. What's the best way to accomplish this?
You can actually render partials from any folder. For example, in users/show.html.erb you can render a partial _info from, say, transactions.
<%= render 'transactions/info' %>
A common thing to do is to put such shared partials into a separate directory with a descriptive name (I use "shared").
<%= render 'shared/filter_panel' %>
you can put them in /views/application/ dir, rails automatically look for partials in this directory
if you use the application directory then you can just do render 'partial' from any view and it will render /views/application/_partial.html.erb
you can also create for e.g. /views/admin/base dir (if you have admin/base_controller.rb) and put your admin namespace partials there
if you have many partials, I recommend you put them into subdirectories
In a fairly large rails application we are running into the issue of overly complex views. Most of the views have too much logic.
Views have between 2 and 4 related instance variables, and make use of various logic checks and helper methods to render the view.
Here is a fake example:
<% if authorized?(#user) %>
<!--some html-->
<% recent_projects(#projects).each do |project| %>
<!-- html & helper methods -->
<% end %>
<!--some more html-->
<% else %>
<!--some html & helper methods-->
<% end %>
Presenters, Facades, and helpers:
I've been researching using the facade and/or presenter pattern to help expose the functionality we need in the view while extracting and isolating the complexity.
My Question is:
Where does the html live? Do I make several small view partials and render them conditionally from helper methods? Do I create a presenter which has methods that handle the logic and output of the html (using content_tag, etc)?
Any general "best practices" or guidance would be appreciated. If I haven't been clear ask questions and I will respond quickly.
I'm unsure what the best practice is specifically but what I think is that making smaller view partials is the way to go so that the view code is separated and can be separately maintained. Your presentation logic resides inside a Presenter class and renders appropriate partials from app/views.
I think that using content_tag is going to make your UI designer think, say, how to add a css class to an element, ultimately leading to more maintenance time. content_tags are fine if they are smaller and are not frequently updated in terms of styling.
Another point to keep in mind is if you are a developer who is also after cleanliness of code then imagine writing a content_tag for multi-line block of <div /> with paragraph of text with multiple inner tags. You'd have to start manipulating strings to handle this and sometimes it become time consuming to yourself and other developers to follow the code.
I'm sure there are many more goodies of keeping the view template separate from Presenters but the point mentioned above is one that I learned from one of my recent projects.
I have a page object and various template objects in my application. pages have names, descriptions urls etc and each have a relationship with a template. templates have different numbers of content boxes and relationships with other controllers (like blogs, galleries etc.).
When I am rendering a page I can work out what template is attached to the page, and what the relevant content is based on that. but I am not sure what the best way is to render the nested Items.
Are you meant to somehow render the templates view from within the other view? Or would you have to just rewrite the view altogether? In this case would I have to create an extra template view for each different template, bundle it with the page views, and then only include it if it is the right one?
Would this be the same for galleries and blogs? do they all need to be bundled with the page? Or can it be called from its proper location?
I'm not sure what the best practice is here and haven't had any luck googling it. I'm suspecting that the key words im using aren't correct. Or this is common knowledge that isn't worth documenting.
You can use shared partials to render views. Check out this guide.
In the views, you can render the partials based upon whatever condition you want.
For example:
- if params[:page] == "my_page"
= render "shared/my_page"
Naturally, you will still need to set up the needed data in the controller.
Shared logic for this can be placed in the Application Controller.
When to choose a partial or helper or a loop in view.
Specifically, when we need a repeated structure only in a single page.
I would like to know which one is a best practice from DRY point of view and Performance point of view.
Thanks.
If only in a single page, as described, I would consider using a loop or partial.
- If the output is one or two lines I might just use a loop.
- If more than that a partial
If used in multiple pages I would use a helper.
Overall I prefer helpers as they feel like a more object oriented, ruby approach and they are easy and great to have tests for!