ROR: Nested views - ruby-on-rails

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.

Related

How can I render a heterogeneous collection of models using special partials contained at a specified path?

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?

How Do I Submit Information from Multiple Partials?

I have a page with a form on it. I dynamically generate the form based on information i have available to me in the database, but it works out to look a little like this:
MainView
-Partial1
-Partial2
--Partial2a
--Partial2b
All of the partials contain parts of the form. I've placed breakpoints in the code, that show me on the controller (in this case "Create"), the object does not have any of the information from the form populated in it.
This is the code i'm using for the 'create' button:
<button asp-action="Create" asp-controller="LSAA">Create</button>
How do I make sure that my post includes the information from the MainView, as well as the information from all the partials rendered as part of that main view?
The main view and the partials all render as one view from a browser, and call resources (js, css.. etc) from the perspective of the main view. If anything goes wrong - it means the configuration of the partials into the main view is probably the issue. When you say you don't get information from the partials do you mean it's not posted to the database? Or do you mean form data from that partial specifically does not appear in the view?

What does the term "template" mean?

I am learning Rails 5.0, via a tutorial. Learning how to generate view templates, but the term "template" is never explicitly defined. I've searched in the Rails docs, and they seem to mention the word a lot, but also never really define it explicitly.
I know that views are the HTML, CSS associated with what the user sees. But was wondering what is a template and how is it different than a standard webpage?
I don't have an authoritative answer. But this is really rather simple. RoR lets you generate content dynamically. This means, with one template, you could generate different content (html pages). The final html page generated and served by the server is the webpage endusers see. For example, you could have a template show.html.erb with the following line:
<h> Product <%=#product.name%> </h>
From this template, different webpages for each different #product can be generated with that #product's name, depending on the #product variable, which is provided by the controller.
So templates allow you to dynamically generate content and render them as different html webpages.

Is there anyway to create a centralized partial in Rails 4?

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

reusing the same table across several views in rails

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.

Resources