才华横溢 workout_log 案例
才华横溢 workout_log 案例 8
cd workspace
rails new workout_log
cd workout_log
rails s
git init
git add .
git commit -m "initial commit"
git remote add origin https://github.com/shenzhoudance/workout_log.git
git push -u origin master
git checkout -b model workout
rails g model workout date:datetime workout:string mood:string length:string
rake db:migrate
rails g controller workouts
config/routes.rb
---
Rails.application.routes.draw do
resources :workouts
root 'workouts#index'
end
---
rails s
app/controllers/workouts_controller.rb
---
class WorkoutsController < ApplicationController
def index
end
end
---
app/views/workouts/index.html.erb
---
<h1>欢迎来到才华横溢的世界</h1>
git add .
git commit -m "add workout controller index & routes"
git checkout -b gem
https://rubygems.org/
gem 'haml', '~> 5.0', '>= 5.0.4'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'simple_form', '~> 3.5', '>= 3.5.1'
bundle install
rails server
---
https://github.com/twbs/bootstrap-sass
app/assets/stylesheets/application.css.scss
---
@import "bootstrap-sprockets";
@import "bootstrap";
---
app/assets/javascripts/application.js
//= require bootstrap-sprockets
//= require turbolinks
---
https://github.com/plataformatec/simple_form
rails generate simple_form:install --bootstrap
---
app/views/jobs/index.html.haml
%h1 欢迎来到才华横溢的领域
rails server
app/controllers/workouts_controller.rb
---
class WorkoutsController < ApplicationController
def index
end
def show
end
def new
end
def create
end
def edit
end
def update
end
def destroy
end
private
def workout_params
end
def find_workout
end
end
---
class WorkoutsController < ApplicationController
def index
end
def show
end
def new
@workout = Workout.new
end
def create
@workout = Workout.new(workout_params)
if @workout.save
redirect_to @workout
else
render 'new'
end
end
def edit
end
def update
end
def destroy
end
private
def workout_params
params.require(:workout).premit(:date,:workout,:mood,:length)
end
def find_workout
end
end
---
rails s
http://localhost:3000/workouts/new
app/views/workouts/_form.html.haml
---
= simple_form_for(@workout, html: { class: 'form-horizontal' }) do |f|
= f.input :date, label: "Date"
= f.input :workout, label: "What area did you workout?", input_html: { class: "form-control" }
= f.input :mood, label: "How were you feeling?", input_html: { class: "form-control" }
= f.input :length, label: "How long was the workout?", input_html: { class: "form-control" }
%br/
= f.button :submit
---
app/views/workouts/new.html.haml
---
%h1 new workout
= render 'form'
= link_to "cancel", root_path
---
rails s
http://localhost:3000/workouts/new
app/views/workouts/show.html.haml
---
#workout
%p= @workout.date
%p= @workout.workout
%p= @workout.mood
%p= @workout.length
= link_to "Back", root_path
= link_to "Edit", edit_workout_path(@workout)
= link_to "Delete", workout_path(@workout), method: :delete, data: { confirm: "Are you sure?" }
---
rails s
http://localhost:3000/workouts/1
app/controllers/workouts_controller.rb
---
class WorkoutsController < ApplicationController
before_action :find_workout, only: [:show, :edit, :update, :destroy]
def index
end
def show
end
def new
@workout = Workout.new
end
def create
@workout = Workout.new(workout_params)
if @workout.save
redirect_to @workout
else
render 'new'
end
end
def edit
end
def update
end
def destroy
end
private
def workout_params
params.require(:workout).permit(:date,:workout,:mood,:length)
end
def find_workout
@workout = Workout.find(params[:id])
end
end
---
---
app/views/workouts/index.html.haml
---
%h1 欢迎来到才华横溢的领域
- @workouts.each do |workout|
%p= workout.date
%p= workout.workout
---
app/controllers/workouts_controller.rb
---
def index
@workouts = Workout.all.order("create_at DESC")
end
---
---
app/views/workouts/index.html.haml
---
%h1 欢迎来到才华横溢的领域
- @workouts.each do |workout|
%h2= link_to workout.date,workouts
%h3= workout.workout
---
rake routes
app/controllers/workouts_controller.rb
---
def update
if @workout.update(workout_params)
redirect_to @workout
else
render 'edit'
end
def destroy
@workout.destroy
redirect_to root_path
end
---
https://getbootstrap.com/docs/3.3/css/
https://getbootstrap.com/docs/3.3/components/
---
app/views/layouts/application.html.haml
---
!!!
%html
%head
%title Workout Log Application
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
= javascript_include_tag 'application', 'data-turbolinks-track' => true
= csrf_meta_tags
%body
%nav.navbar.navbar-default
.container
.navbar-header
= link_to "Workout Log", root_path, class: "navbar-brand"
.nav.navbar-nav.navbar-right
= link_to "New Workout", new_workout_path, class: "nav navbar-link"
.container
= yield
app/assets/stylesheets/application.scss
---
@import "bootstrap-sprockets";
@import "bootstrap";
html {
height: 100%;
}
body {
background: -webkit-linear-gradient(90deg, #616161 10%, #9bc5c3 90%); /* Chrome 10+, Saf5.1+ */
background: -moz-linear-gradient(90deg, #616161 10%, #9bc5c3 90%); /* FF3.6+ */
background: -ms-linear-gradient(90deg, #616161 10%, #9bc5c3 90%); /* IE10 */
background: -o-linear-gradient(90deg, #616161 10%, #9bc5c3 90%); /* Opera 11.10+ */
background: linear-gradient(90deg, #616161 10%, #9bc5c3 90%); /* W3C */
}
.navbar-default {
background: rgba(250, 250, 250, 0.5);
-webkit-box-shadow: 0 1px 1px 0 rgba(0,0,0,.2);
box-shadow: 0 1px 1px 0 rgba(0,0,0,.2);
border: none;
border-radius: 0;
.navbar-header {
.navbar-brand {
color: white;
font-size: 20px;
text-transform: uppercase;
font-weight: 700;
letter-spacing: -1px;
}
}
.navbar-link {
line-height: 3.5;
color: rgb(48, 181, 199);
}
}
#index_workouts {
h2 {
margin-bottom: 0;
font-weight: 100;
a {
color: white;
}
}
h3 {
margin: 0;
font-size: 18px;
span {
color: rgb(48, 181, 199);
}
}
}
git checkout -b exercise
rails g model exercise name:string sets:integer reps:integer workout:references
rake db:migrate
app/models/workout.rb
---
class Workout < ApplicationRecord
has_many :exercises
end
---
rake routes
rails g controller exercises
---
class ExercisesController < ApplicationController
def create
@workout = Workout.find(params[:workout_id])
@exercises = @workout.exercises.create(params[:exercise].permit(:name, :sets, :reps))
redirect_to workout_path(@workout)
end
end
---
app/views/exercises/_form.html.haml
---
= simple_form_for([@workout, @workout.exercises.build]) do |f|
= f.input :name, input_html: { class: "form-control" }
= f.input :sets, input_html: { class: "form-control" }
= f.input :reps, input_html: { class: "form-control" }
%br/
= f.button :submit
---
app/views/exercises/_exercises.html.haml
---
%p= exercise.name
%p= exercise.sets
%p= exercise.reps
---
https://hackhands.com/format-datetime-ruby/
git checkout -b uigradients
https://uigradients.com/#Shift
http://css3generator.com/
git checkout -b heroku
---
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'sqlite3', '1.3.11'
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
end
group :development do
gem 'pg', '0.18.4'
end