0
0
Ruby on Railsframework~5 mins

Why views present data in Ruby on Rails

Choose your learning style9 modes available
Introduction

Views show data to users in a clear and friendly way. They turn raw information into pages people can read and understand.

When you want to display a list of products on a webpage.
When showing user profile details like name and email.
When presenting search results after a user submits a query.
When you need to format dates or numbers nicely for users.
When you want to separate how data looks from how it is stored or processed.
Syntax
Ruby on Rails
<%= @variable %>
Use <%= %> to insert Ruby data into HTML in a view.
Instance variables like @variable come from the controller.
Examples
Shows the user's name inside a heading.
Ruby on Rails
<h1>Welcome, <%= @user.name %>!</h1>
Displays the current date formatted nicely.
Ruby on Rails
<p>Today is <%= Date.today.strftime('%B %d, %Y') %>.</p>
Loops through items and shows each title in a list.
Ruby on Rails
<ul>
  <% @items.each do |item| %>
    <li><%= item.title %></li>
  <% end %>
</ul>
Sample Program

This example shows a controller setting a message, and the view displaying it inside a paragraph.

Ruby on Rails
# app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
  def index
    @message = "Hello, Rails learner!"
  end
end

# app/views/welcome/index.html.erb
<h1>Message for you:</h1>
<p><%= @message %></p>
OutputSuccess
Important Notes

Views should only display data, not change it.

Keep logic in controllers or helpers, not in views.

Use partials to reuse view code and keep things tidy.

Summary

Views turn data into readable pages for users.

They use special tags to insert Ruby data into HTML.

Controllers prepare data, views present it clearly.