What is the use of partials in Rails? - ruby-on-rails

In Rails why would I ever use partials? Explain it as if I'm 5 years old. I simply do not get why anyone would ever use them.

First thing, Please have a read here Official Rails guide for partials .
Now some benefits
It keeps your view clean and systematic, DRY Philosphy .
The most important where
partial comes into picture is when you want to reuse some of the
component amongst various view. Usually developer create a kind of
shared/common folder where partial sits and are used amongst
various view.
It’s also easy to conditionally load partials using Rails’ “if” or “unless” statements
It is beneficial where a template needs to iterate over a collection and render a sub template for each of the elements.
Your different partials can also have different layouts.
The Partial API here list all the various methods which will make you understand it's benefit.
Separating your view into partials can also help in in proper fragment caching (Fragment Caching) of some portion of you webpage. Better management.
If you are into Metaprogramming then you can add that flavor in your partials too, by creating dynamic helpers. As Stackoverflow answer here

They are a handy way to avoid repeating yourself.
For example, you may have several pages that display a menu. Instead of repeating the markup for the menu in every view, you just throw it in a partial and render on every page.
There are other cases where complex views are made more manageable by breaking them into several partials.

You normally use partials to reuse code: let's say you have a list of posts and each post has a small preview with image, title and excerpt. In your blog homepage you have a list of posts, when you group posts by year, you show a list of posts, when you search for a term you have to display a list of posts.
Instead of repeating the logic to display a post preview, you can move that logic to a partial and keep referring to that whenever you need it. You can keep your code DRY and write less code. Moreover, if you realize you want to add something new, you can just change the partial instead of going and hunting for the templates which display the post previews all over your application.

Well, for the exact same reason that you want to use methods, which is reusing code. Say you have a status area in your application, that you want to show in different places. You can just put the view code for that status area in a partial and then use that partial on the corresponding pages.
Since partials can also take parameters, they make it very easy to reuse view code. Also, you can make partials dedicated for certain models of your application. This way, you can just call render #model and the correct partial is picked by naming conventions.

A partial allows you to separate layout code out into a file which will be reused throughout the layout and/or multiple other layouts.
For example, you might have a login form that you want to display on 10 different pages on your site. Rather than writing the form code 10 times, you can write it once in a partial, and in each of the 10 pages, simply include that partial at the appropriate place in the layout. If necessary, you can pass local instance variables to the partial to make them available to it.
That way, if you need to change the form, you only need to change the code in the partial, one time, rather than changing it 10 times across all of your layouts.
Here is the guide on partials: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
And more discussion on getting data/variables into partials: Pass a variable into a partial, rails 3?

Related

Bad idea to manually edit a scaffolded view?

I scaffolded a new MVC controller, along with a corresponding view and repository for an IEnumerable collection of a POCO class. In the resulting view is a table that will list every entry in the collection of that class. However, the table also contains a column for every property in that class, and I only want to display a subset of them in this view. Is it a bad idea if I now manually edit the view to remove what I don't want to display in the generated HTML?
I'm not clear on whether the scaffolding features that are part of ASP.NET MVC (version 3 and later) are designed to generate the basic functionality for your web application, giving you the freedom to do what you want with the results afterwards, or if it's considered a best practice to leave the generated results alone and find another way to do what I want to accomplish. I know that you can customize your own scaffolding templates, but that seems like a lot of work for such a simple change.
Thanks!
It's definitely OK to edit the scaffolded view. This is just a feature that's there to quickly build the basics, but that will never suffice.
So you can just edit it all you want. Keep in mind though that if you edit your model, you will have to manipulate your view manually, but that's normal I think.
Scaffold will HELP you to generate a View strongly typed to a Model. Of course, if you want, you can update the html in your views. Another good tip: Instead of using your Model, create a ViewModel based on this Model, and put only the properties you need to display in the View.
View this content:
https://stackoverflow.com/a/11064362/1384539

Rails: Making widgets

I want to create a widget-like entity for my Rails app. For example, I'd like to be able to render a list of products marked as featured and have it work in any view file.
My first thought was to try to do this using a partial. Is this the best way? Will I be able to achieve my overall goal of having a widget I can inject anywhere? What's the best way to implement this?
Markup that is shared across many view files should be placed in a shared partial.
In this case, your partial will depend on having a collection of products available. You will need to query this collection in each controller action that corresponds to a page containing the widget. This sounds like a good way to do it to me.

How to create a big view (page) in MVC?

I am new to MVC and learning some stuff of views, partial views etc.
I would like to make a simple page where lets consider "Login" and "Register" both comes together.
Like facebook.
I have found all the examples which is all having one view on page. I guess, to make a big view (complete page) we need to have partical views to use in one big view.
unfortunately, i am not able to find perfect example, may be lake of searching power or exact term to use for MVC.
I want to make a page where many partial views use in one big view and create a big page.
Can any one please help me to get video link or help link which can be useful and how exactly it works?
Check out the partial views in
http://www.pluralsight-training.net/microsoft/players/PSODPlayer?author=scott-allen&name=mvc3-building-views&mode=live&clip=0&course=aspdotnet-mvc3-intro
Using #Html.Partial is fairly easy, I generally prefer #Html.Action to render another controllers action method in line, and it makes more sense for me in a situation like you mentioned above.
Something else to consider are the ajax type dialogs for login, now in the mvc4 developer preview templates at: http://www.asp.net/mvc/mvc4

How do I create composite views in ASP.Net MVC 3?

How do I create a view in ASP.Net MVC 3 that defers creation/rendering of a portion of the view to another view/controller, without knowing at design time what view/controller will be deferred to?
I have a Dashboard view/controller that uses a Layout page. the dashboard view does most of the work of rendering the overall screen. However a certain placeholder section of the overall screen will differ based on business rules, configuration, user interaction etc. There are three separate plugin views/controllers that may need to be rendered in the placeholder section. How do I instruct the Dashboard view to render a specific plugin view once the Dashboard controller has determined which plugin should be visible. I have accomplished this already by putting flags in the Dashboard model and then having conditional logic in the Dashboard view call Html.RenderAction with the appropriate parameters based on the flags. This seems unsatisfactory and I would assume there is already a simple built in functionality for this kind of scenario, please advise.
Thank you
You are really asking "how do I dynamically render partial views who's identity is known at design time."
One can get there two ways, either through Html.RenderPartial or Html.RenderAction. And both have their place. If the main page "knows" enough to have the view data, then RenderPartial is your the better bet as it is a bit more efficient than RenderAction. If you only know, say, a unique identifier for your portal widget than RenderAction works because you've now got a fully capable controller method that can do things like go back to a database and render a view.
You could write your own htmlhelper and use that in your view instead of using the flags.

Best practices when including/using a "partial view"?

In modern web frameworks like Rails and symfony the concept of partial includes or partial views is well documented and recommended.
What I am having trouble with lately is deciding how much design to include in the partial.
It's kind of hard to explain but I want to know what others do when creating a partial and including it in a template. Do you only display the data and position it in the template or do you put all the styling and positioning code in the partial and just include it like so.
I guess my question is, what is your thought process when deciding to create a partial and when do you use it in your own code, and how much do you put into your partial when you decide to use one.
http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer#Partials
http://api.rubyonrails.org/classes/ActionView/Partials.html
I use partials for pieces of view code that are used in more than one place. If the code is going to be duplicated or re-used in several places, then it is a prime candidate to be DRY'd up and placed into a partial.
As far as styling code, all that should reside in your CSS files
If you're repeating parts of your view code, a partial is a good idea. If the code you're reusing is a little different between your pages, make use of the parameters you can pass the partial.
As for the CSS, I'd suggest keeping it outside your partial/template. If you have a lot of partial specific CSS code, create a "partials" folder under web/css and name the css files to match your partials (If you think the overhead is worth the organization).
I use partials in two cases:
When I need to re-use views
When a view is huge (hundreds of lines), I split it into partials to make it more descriptive and thereby easier to maintain.

Resources