Skip to content

Instantly share code, notes, and snippets.

@lsylvester
Created April 22, 2014 10:51
Show Gist options
  • Save lsylvester/11173934 to your computer and use it in GitHub Desktop.
Save lsylvester/11173934 to your computer and use it in GitHub Desktop.
# application.html.erb
<html>
<head>
<%= yield :title, wrapper: :title %>
</head>
<body>
<%= yield %>
</body>
</html>
# application/index.html.erb
<% content_for(:title) do %>
My Site
<% end %>
<%= link_to 'Posts', posts_path %>
# posts.erb
<% content_for(:title) %>
Posts
<% end %>
<ul>
<% @posts.each do |post| %>
<li><%= post.title %></li>
<% end %>
</ul>
<%= yield %>
# posts/index.erb
<h1>Select a Post</h1>
# post.html.erb
<% content_for(:title) %>
<%= @post.title %>
<% end %>
<h1><%= @post.title%></h1>
<%=raw @post.body %>
# controllers/posts_controller.rb
clas PostsController < BaseController
def posts
@posts ||= Post.all
end
end
# controllers/post_controller.rb
class PostController < BaseController
def post
@post ||= Post.find(params[:id])
end
end
# GET / Accept html
<html>
<head>
<title data-content='title'>My Site</title>
</head>
<body>
<div data-path='index'>
<a href='/posts'>Posts</a>
</div>
</body>
</html>
# GET / Accept json
{
path: 'index'
content: "<a href='/posts'>Posts</a>"
title: "My Site"
}
# GET /posts Accept html
<html>
<head>
<title data-content='title'>Posts</title>
</head>
<body>
<div data-path='posts'>
<ul>
<li><a href='/posts/1'>Hello World</a></li>
<li><a href='/posts/2'>Godbye Cruel World</a></li>
</ul>
<div data-path='posts/index'>
<h1>Select a Post</h1>
</div>
</div>
</body>
</html>
# GET / Accept json ref='index'
{
path: 'posts/index'
content: "<ul>
<li><a href='/posts/1'>Hello World</a></li>
<li><a href='/posts/2'>Godbye Cruel World</a></li>
</ul>
<div data-path='posts/index'>
<h1>Select a Post</h1>
</div>"
title: "Posts"
}
# GET / Accept json ref='posts/1'
{
path: 'posts/index'
content: "<h1>Select a Post</h1>"
title: "Posts"
}
# GET /posts/1 Accept html
<html>
<head>
<title data-content='title'>Hello World</title>
</head>
<body>
<div data-path='posts'>
<ul>
<li><a href='/posts/1'>Hello World</a></li>
<li><a href='/posts/2'>Godbye Cruel World</a></li>
</ul>
<div data-path='posts/post:1'>
<h1>Hello World</h1>
<p>Greetings to the World</p>
</div>
</div>
</body>
</html>
# GET / Accept json ref='index'
{
path: 'posts/post:1'
content: "<ul>
<li><a href='/posts/1'>Hello World</a></li>
<li><a href='/posts/2'>Godbye Cruel World</a></li>
</ul>
<div data-path='posts/index'>
<ul>
<li><a href='/posts/1'>Hello World</a></li>
<li><a href='/posts/2'>Godbye Cruel World</a></li>
</ul>
<div data-path='posts/post:1'>
<h1>Hello World</h1>
<p>Greetings to the World</p>
</div>"
title: "Posts"
}
# GET /posts/1 Accept json ref='posts/index'
{
path: 'posts/post:1'
content: "<h1>Hello World</h1>
<p>Greetings to the World</p>"
title: "Hello World"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment