0Pricing
Ruby Academy · Lesson

Views and Rendering

Returning responses.

Views and Rendering is a free Ruby Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a View?

A view is a template that produces the response sent to the client, usually HTML. It turns the data a controller prepared into a page.

  • Views live in app/views.
  • They are grouped by controller, like app/views/articles.
  • The default template name matches the action.

Convention-Based Rendering

By default an action renders the template named after it. The index action of ArticlesController renders app/views/articles/index.html.erb with no explicit render call.

def index
  @articles = Article.all
  # automatically renders articles/index.html.erb
end

ERB Templates

The default template language is ERB (Embedded Ruby). It mixes HTML with Ruby:

  • <%= ... %> outputs a value.
  • <% ... %> runs code without output.
<h1>Articles</h1>
<% @articles.each do |article| %>
  <p><%= article.title %></p>
<% end %>

Layouts

A layout wraps every view with shared markup like the header, navigation, and footer. The default app/views/layouts/application.html.erb includes <%= yield %>, where the action's view is inserted.

<!-- application.html.erb -->
<html>
  <body>
    <%= yield %>
  </body>
</html>

Partials

A partial is a reusable template fragment whose filename starts with an underscore. Render it with render to avoid duplicating markup.

<!-- _article.html.erb -->
<p><%= article.title %></p>

<!-- in index.html.erb -->
<%= render "article", article: @article %>

Rendering Collections

Passing a collection to render renders the partial once per item, an idiomatic shortcut. Rails infers the partial name from the model.

<%= render @articles %>

<!-- renders _article.html.erb for each article -->

View Helpers

Rails provides helpers that generate HTML safely: link_to for links, form_with for forms, and image_tag for images. They keep templates clean and consistent.

<%= link_to "View", article_path(@article) %>
<%= image_tag "logo.png" %>

Responding with Other Formats

Controllers can return more than HTML. render json: sends JSON for APIs, and respond_to serves different formats based on what the client requested.

def show
  @article = Article.find(params[:id])
  render json: @article
end

respond_to Blocks

Use respond_to to handle multiple formats in one action. The same action can return HTML to a browser and JSON to an API client.

def show
  @article = Article.find(params[:id])
  respond_to do |format|
    format.html
    format.json { render json: @article }
  end
end

Status Codes

Set the HTTP status on a response with status:. Use :ok (200), :created (201) after a successful create, and :unprocessable_entity (422) when validation fails so clients react correctly.

render :new, status: :unprocessable_entity
render json: @article, status: :created

Escaping and Safety

ERB automatically HTML-escapes <%= %> output, which prevents cross-site scripting from user data. Only mark content as safe with raw or html_safe when you fully trust it; misusing them reopens XSS holes.

Quick Check

Test your understanding of views and rendering.

Recap: Views and Rendering

You learned to return responses:

  • Actions render a matching ERB view by convention.
  • Layouts wrap views; partials reuse fragments.
  • Helpers like link_to and form_with build HTML.
  • render json: and respond_to serve APIs; set proper status codes.
  • ERB escapes output to prevent XSS.

That completes the Controllers and Routing course.

Frequently asked questions

Is the “Views and Rendering” lesson free?

Yes — the full text of “Views and Rendering” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Views and Rendering”?

Returning responses. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Views and Rendering” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Routes and Resources
  2. Controllers and Actions
  3. Strong Parameters
  4. Views and Rendering
← Back to Ruby Academy