Include variable definitions in multiple views - ruby-on-rails

In a number of views I have the following kind of code:
<% compute a number of variables %>
Do something with those variables to layout and configure HTML elements.
I'm drying to dry up my views. That first part where I compute the values for the variables is used in a number of different views. I would like to be able to pull it out into another file and use it in each view that needs it so that I only have to change it in one place. If I pull it into its own partial and then render that partial in another view, the variables are not defined in the view. To be more specific:
<%
top = 0
link.siblings_before.each do |sibling|
top = top + compute_top(sibling)
end
%>
<div style="position:absolute;top:<%= top %>px"><%= link.name %></div>
I want to be able to use the computation of top in more than one view without having to cut and paste the ruby code into each view.

Try avoiding logic in your views.
You can define a before_filter in your controller and enabled it for multiple actions that you need them for.
Those variables defined in your before filter will be available in those views and you keep your views clear at the same time.

Related

Use single view with two different layouts in Razor

This might be a weird question, but I have two layout files in my project and I have a single view I'd like to render separately in each layout file (depending on URL).
So the issue I have is when I define the sections in the view. Both layout pages have similarly functioning sections, but they are named different. For example, if I wanted to use layout1 it would be #section main1 and for layout2 it would be #section main2.
The view will render the same content within these sections, so can I dynamically set the section name rather than making two copies of the same content with just the section name changed?
You can easily change the layout page by setting the layout you want to use, but you can't dynamically set the name to my knowledge. If you really can't change the section names of one of them, what you could do is create a nested layout of the one, and in that nested layout define section names that match in the nested one.
Basically you define:
#section a
{
#RenderSection("namethatmatchesotherlayout")
}
And through this you can get something that matches.

Rendering two views at the same time, or a partial and a view

I'm working on a site right now where I have a standard layout, with a view/template. I've spent the past 2 weeks trying to get ajax functionality working, but it's not working and I need an alternative. Is it possible to render a partial WITHIN a view?
As in, layout (view (partial)) sort of thing?
Right now I just have a standard layout and view with a yeild in the layout. There is an empty div in the view where the ajax HTML was supposed to go, I want to fill it with a partial for now
Yes surely, you can call a partial inside any view file. For example, you have a view file 'index.html.erb' and you want to call a partial '_new_partial.html.erb'
In your view file.
<div>
<%= render :partial => 'new_partial' %> #provide complete path if partial is not in the same directory.
</div>

Rails: Nested views or partials or..?

I new to Ruby on Rails, and I am coming from Drupal/PHP background.
I am trying to do something simple. A "Static" controller for some pages like about us, disclaimer etc. and a "Video" controller for videos.
The whole application has same look. I am trying to figure out how I can avoid duplicating views.
In drupal the theming goes like this:
html.tpl.php (renders page.tpl.php)
- page.tpl.php (renders multiple block.tpl.php)
- block.tpl.php (renders more nested tpls or some html generated by theme functions).
I find this to be great.
In rails, what I read is there is application.html.erb (Which I think is similar to html.tpl.php of Drupal, as it normally contains stylesheets, js, etc. available throughout the application) called "Layout". Then there are "Views", which "yield" content in this layout. We specify the view in controller, or it gets one automatically. I think of a view as "page.tpl.php" of Drupal.
Most of my site uses same structure i.e. header, top menu, content, right sidebar, footer. But content of each area changes depending on the path. So I am thinking of doing it like this:
Both 'Static' & 'Video' will call the same view, with different variable values i.e. values for content area and sidebar.
This View will the use 'render' to render another view, with a subset of variables passed from controller i.e. it will render content & sidebar views.
If needed, those views will render more views, and so on.
Another one I read about is partials. I can't seem to differentiate between rendering a view inside another view, and rendering a partial.
Which method is better? Or is there a better "Rails Way"?
This is quite a large topic to cover but edited highlights are
If you want to repeat content on every page, like a header or navbar for example then put it in layouts/application.html.erb
if you want to keep your views tidy with content specific to that page then you can use partials. Normally in views I create a folder called shared and then put my partials in there. for example if i save a partial called _form.html.erb (make sure you name partials with the underscore at the beginning of the file name), i would then call that in my view like
<%= render 'shared/form' %>
you can also use partials in the layouts/application file to keep that clean aswell
This is just my way of doing it, I'm sure more experienced rails guys have better ways
Hope that helps, any more questions then just ask

HTML Helpers and Partial Views

If I have say a Partial View called MypartialView and I have a HTML Helper called "MyHTMLHelper" how can I return a partial view from the helper?
My requirement is that sometimes I'd like to render a PartialView on it's own and other times I'd like to render it with another partial view, or a slab of text or something.
So I thought I could create a helper that return both partial views, and a html helper that would return the partial view along with a slab of text.
is this best practice or should i instead create a partial view that has both partials in it and another that has a the partial view + the slab of text?
I'm not only looking for source but also the best practice according to what people are doing.
thanks.
I would use two Views:
-With 2 to partials
<% Html.RenderPartial("Partial1"); %>
<% Html.RenderPartial("Partial2"); %>
-The Partial and some text
Some Text
<% Html.RenderPartial("Partial1"); %>
I think the concept of DRY is still there, because at the end you still have all the code in one place, the Partial Views, and you just reference it from another two Views.
Doing it the other way will be complicated, and I don't think its really necessary to use another Helper Method to accomplish this.
Helpers seem to be designed to be reused a lot more heavily than partials so i'd suggest that if you think you will use the rendered result from the helper as much as you would with the alternative method (nested PV) then go with the helper.

ViewData not inheriting in partials

I was trying to use a shared partial view to render when a particular listing page has no data. I wanted to use ViewData to pass information from the page into my listing control, which would then conditionally render the NoData partial view using the ViewData values.
I would like to be able to specify them in the view markup, not in the controller action, but when I add them in the view the don't seem to inherit down into child partial views (like the Nodata partial view). However, specifying them in the ViewData values in the controller actions works fine, the data is available all the way down...
Does anyone know why it behaves this way?
When rendering a partial you can also pass the ViewData.
<% Html.RenderPartial("NoData", ViewData); %>
<%Html.RenderPartial("partialViewName", "viewData", "model"); %>
it is a best practice to do the decision inside the controller, if you have a scenario to make a decision inside the view, separate them and call them inside the controller conditionally

Resources