view Rails
Estimated reading time: 2 minuteswhat is view
rails generate scaffold article
parts of views
- template
- partials
- layouts
~ extension is .erb
that mean embedded ruby
basic
<% %> |
<%= %> |
---|---|
no output | output |
conditions, loops or blocks | html |
execute ruby | output as view |
partials
<%= render "menu" %>
render
render :edit
render action: :edit
render "edit"
render "edit.html.erb"
render action: "edit"
render action: "edit.html.erb"
render "books/edit"
render "books/edit.html.erb"
render template: "books/edit"
render template: "books/edit.html.erb"
render "/path/to/rails/app/views/books/edit"
render "/path/to/rails/app/views/books/edit.html.erb"
render file: "/path/to/rails/app/views/books/edit"
render file: "/path/to/rails/app/views/books/edit.html.erb"
render inline: "<% products.each do |p| %><p><%= p.name %></p><% end %>"
render plain: "OK"
render html: "<strong>Not Found</strong>".html_safe
render json: @product
render xml: @product
render js: "alert('Hello Rails');"
render body: "raw"
render status: 500
render status: :forbidden
Structuring Layouts
- Assets tag
- yeild tag (content_for)
- partials
Assets tag
tag | tag | tag |
---|---|---|
auto_discovery_link_tag | javascript_include_tag | stylesheet_link_tag |
image_tag | video_tag | audio_tag |
javascript_include_tag
<%= javascript_include_tag "main" %>
# output
<script src='/assets/main.js'></script>
<%= javascript_include_tag "main", "columns" %>
<%= javascript_include_tag "https://example.com/main.js" %>
stylesheet_link_tag
<%= stylesheet_link_tag "main" %>
<%= stylesheet_link_tag "main", "photos/columns" %>
# output
app/assets/stylesheets/main.css and app/assets/stylesheets/photos/columns.css
# direct cdn
<%= stylesheet_link_tag "http://example.com/main.css" %>
image_tag
<%= image_tag "icons/delete.gif" %>
<%= image_tag "icons/delete.gif", {height: 45} %>
<%= image_tag "home.gif", alt: "Home" %>
# full demo of image tag
<%= image_tag "home.gif", alt: "Go Home", id: "HomeImage", class: "nav_bar" %>
video_tag
<video src="/videos/movie.ogg" />
audio_tag
<%= audio_tag "music/first_song.mp3" %>
yield
yield is a inserted process.
<html>
<head>
<%= yield :head %>
</head>
<body>
<%= yield %>
</body>
</html>
content_for
this is use for named yeild :head
<% content_for :head do %>
<title>This is a content for part</title>
<% end %>