View

Action View is the V in MVC. View responsible for displaying data. It doesn't make any logic operations. Only displaying data.

Responses

From the controller's point of view, there are three ways to create an HTTP response:

Default rendering

config/routes.rb app/controllers/posts_controller.rb

.erb

app/views/posts/index.html.erb

ActionController::Base#render method

In most cases, the ActionController::Base#render method does the heavy lifting of rendering your application's content for use by a browser. There are a variety of ways to customize the behavior of render. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.

Rendering Nothing

Rendering an Action's View

Rendering an Action's Template from Another Controller

Rendering an Arbitrary File

Rendering an inline code

Rendering a string

Rendering JSON and XML

to_json, to_xml methods

Rendering Vanilla JavaScript

Render


The :content_type option

The :status option

The :location option

The :layout option

Layout

app/controllers/posts_controller.rb app/controllers/posts_controller.rb

Layout

app/controllers/posts_controller.rb app/controllers/posts_controller.rb app/controllers/posts_controller.rb

Layout inheritance

app/controllers/application_controller.rb app/controllers/posts_controller.rb

Yield

app/views/layouts/application.html.erb

Content for

Partials

Locals

Collection

app/views/posts/index.html.erb app/views/posts/_post.html.erb

Spacer template

Form helpers

form_tag, label_tag, text_field_tag, hidden_field_tag, submit_tag

checkbox_tag

You can find more details here

Form for

Nested attributes

app/modals/post.rb app/views/posts/_form.html.erb

Nested attributes

Testing views

spec/views/posts/index_spec.rb app/views/posts/index.html.erb
spec/views/posts/show_spec.rb
app/views/posts/show.html.erb

Helpers

app/helpers/application_helper.rb

helper_method

app/controllers/application_controller.rb

Testing helpers

spec/helpers/posts_spec.rb app/helpers/posts_spec.rb

Haml

The principles of Haml:

Haml

views/posts/post.html.erb views/posts/post.html.haml REMEMBER: Do not use spaces between tag and '='

Attributes

views/posts/post.html.haml

Boolean attributes

Haml HTML Haml HTML

Inserting JavaScript

Class and ID: . and #

Haml HTML

/

#