Store#index
Find me in app/views/store/index.html.erb
8.2 Iteration C2: Add a Page Layout 7.3 Playtime
Show the model, view, and controller working together.
Create a second controller with a single index action
rails generate controller Store index
create app/controllers/store_controller.rb
route get 'store/index'
invoke erb
create app/views/store
create app/views/store/index.html.erb
invoke test_unit
create test/controllers/store_controller_test.rb
invoke helper
create app/helpers/store_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/store.coffee
invoke scss
create app/assets/stylesheets/store.scss
Route the 'root' of the site to the store
edit config/routes.rb
Rails.application.routes.draw do
root 'store#index', as: 'store_index'
resources :products
# For details on the DSL available within this file, see
# http://guides.rubyonrails.org/routing.html
end
Demonstrate that everything is wired together
get /
Find me in app/views/store/index.html.erb
In the controller, get a list of products from the model
edit app/controllers/store_controller.rb
class StoreController < ApplicationController
def index
@products = Product.order(:title)
end
end
In the view, display a list of products
edit app/views/store/index.html.erb
<% if notice %>
<aside id="notice"><%= notice %></aside>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<ul class="catalog">
<% @products.each do |product| %>
<li>
<%= image_tag(product.image_url) %>
<h2><%= product.title %></h2>
<p>
<%= sanitize(product.description) %>
</p>
<div class="price">
<%= product.price %>
</div>
</li>
<% end %>
</ul>
Add some basic style
edit app/assets/stylesheets/store.scss
// Place all the styles related to the Store controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
.store {
max-width: 80em;
ul.catalog {
border-top: solid 0.250em;
list-style: none;
padding: 0;
margin: 0;
li {
padding: 1em;
margin: 0;
border-bottom: solid thin #ddd;
// This makes sure our <li> has enough height
// to hold the entire image, since it's floated
&::after {
clear: both;
content: " ";
display: block;
}
img {
float: left;
padding: 1em;
margin-right: 1em;
margin-bottom: 1em;
box-shadow: 0.176em 0.176em 0.354em 0px rgba(0,0,0,0.75);
}
.price {
font-size: 1.414em;
}
}
}
}
Show our first (ugly) catalog page
get /
Powerful, Effective, and Efficient Full-Stack Web Development As a Rails developer, you care about user experience and performance, but you also want simple and maintainable code. Achieve all that by embracing the full stack of web development, from styling with Bootstrap, building an interactive user interface with AngularJS, to storing data quickly and reliably in PostgreSQL. Take a holistic view of full-stack development to create usable, high-performing applications, and learn to use these technologies effectively in a Ruby on Rails environment.
Why Ruby Is Slow, and How to Fix It You don’t have to accept slow Ruby or Rails performance. In this comprehensive guide to Ruby optimization, you’ll learn how to write faster Ruby code—but that’s just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex Dymo will guide you through perils of memory and CPU optimization, profiling, measuring, performance testing, garbage collection, and tuning. You’ll find that all those “hard” things aren’t so difficult after all, and your code will run orders of magnitude faster.
Native Apps, Multiple Platforms Answer the question “Can we build this for ALL the devices?” with a resounding YES. This book will help you get there with a real-world introduction to seven platforms, whether you’re new to mobile or an experienced developer needing to expand your options. Plus, you’ll find out which cross-platform solution makes the most sense for your needs.