Views and Rendering
Returning responses.
Views and Rendering is a free Ruby Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
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
endERB 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
endrespond_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
endStatus 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: :createdEscaping 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_toandform_withbuild HTML. render json:andrespond_toserve APIs; set proper status codes.- ERB escapes output to prevent XSS.
That completes the Controllers and Routing course.
Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Views and Rendering” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Views and Rendering”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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
- Routes and Resources
- Controllers and Actions
- Strong Parameters
- Views and Rendering