| Start |
|---|
| Structure As Rails follows the MVC pattern Let's say you have a controller This structure allows you to have a folder for each controller. When calling an action in your controller the appropriate view will be rendered automatically. |
| Partials Partial templates (partials) are a way of breaking the rendering process into more manageable chunks. Partials allow you to extract pieces of code from your templates to separate files and also reuse them throughout your templates. To create a partial, create a new file that begins with an underscore: To render a partial as part of a view, use the render method within the view:
To pass a variable into the partial as a local variable, use this notation:
Partials are also useful when you need to reuse exactly the same code (DRY philosophy). For example, to reuse Object PartialsObjects that respond to A local named Collections of objects that respond to Global PartialsTo create a global partial that can be used anywhere without referencing its exact path, the partial has to be located in the For example, this is a path to a global partial To render this global partial anywhere, use |
| AssetTagHelper To let rails automatically and correctly link assets (css/js/images) in most cases you want to use built in helpers. (Official documentation (opens new window)) Image helpersimage_pathThis returns the path to an image asset in image_urlThis returns the full URL to an image asset in image_tagUse this helper if you want to include an JavaScript helpersjavascript_include_tagIf you want to include a JavaScript-file in your view. javascript_pathThis returns the path of your JavaScript-file. javascript_urlThis returns the full URL of your JavaScript-file. Stylesheet helpersstylesheet_link_tagIf you want to include a CSS-file in your view. stylesheet_pathThis returns the path of you stylesheet asset. stylesheet_urlThis returns the full URL of you stylesheet asset. Example usageWhen creating a new rails app you will automatically have two of these helpers in This outputs: |
| Replace HTML code in Views If you ever wanted to determine the html content to be printed on a page during run time then, rails has a very good solution for that. It has something called the content_for which allows us to pass a block to a rails view. Please check the below example, Declare content_for |
| HAML - an alternative way to use in your views HAML (HTML abstraction markup language) is a beautiful and elegant way to describe and design the HTML of your views. Instead of opening- and closing tags, HAML uses indentation for the structure of your pages. Basically, if something should be placed within another element, you just indent it by using one tab stop. Tabs and white space are important in HAML, so be sure that you always use the same amount of tabs. Examples: And in HAML: You see, the structure of the layout is much clearer than using HTML and ERB. Installation Just install the gem using and add the gem to the Gemfile For using HAML instead of HTML/ERB, just replace the file extensions of your views from Quick tipps Common elements like divs can be written in a short way HTML HAML HAML, shorthand Attributes HTML HAML Inserting ruby code You can insert ruby code with the = and - signs. Code starting with = will be executed and embedded into the document. Code starting with - will be executed, but not inserted into the document. Full documentation HAML is very easy to start with, but is also very complex, so that I'll recommend reading the documentation (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference