Add more workouts functionality and tests
This commit is contained in:
parent
10d4075f48
commit
456a071bf3
8 changed files with 114 additions and 18 deletions
|
@ -1,3 +1,5 @@
|
|||
# Place all the behaviors and hooks related to the matching controller here.
|
||||
# All this logic will automatically be available in application.js.
|
||||
# You can use CoffeeScript in this file: http://coffeescript.org/
|
||||
$ ->
|
||||
$("#workout_date").datepicker({ dateFormat: 'yy-mm-dd' });
|
||||
|
|
|
@ -10,4 +10,37 @@ class WorkoutsController < ApplicationController
|
|||
def edit
|
||||
@workout = Workout.find(params[:id])
|
||||
end
|
||||
|
||||
def update
|
||||
params.permit!
|
||||
@workout = Workout.find(params[:id])
|
||||
|
||||
if @workout.update_attributes(params[:workout])
|
||||
redirect_to @workout, notice: 'Workout updated'
|
||||
else
|
||||
render action: 'edit'
|
||||
end
|
||||
end
|
||||
|
||||
def new
|
||||
@workout = Workout.new
|
||||
end
|
||||
|
||||
def create
|
||||
params.permit!
|
||||
@workout = Workout.new(params[:workout])
|
||||
|
||||
if @workout.save
|
||||
redirect_to @workout, notice: 'Workout created'
|
||||
else
|
||||
render action: 'new'
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@workout = Workout.find(params[:id])
|
||||
@workout.destroy
|
||||
|
||||
redirect_to workouts_url, notice: 'Workout deleted'
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
class Workout < ActiveRecord::Base
|
||||
validates :date, :presence => true
|
||||
end
|
||||
|
|
|
@ -1,4 +1,11 @@
|
|||
<%= form_for @workout do |f| %>
|
||||
<% if @workout.errors.any? %>
|
||||
<ul>
|
||||
<% @workout.errors.full_messages.each do |message| %>
|
||||
<li><%= message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
<%= f.label :date %>
|
||||
<%= f.text_field :date %>
|
||||
<%= f.submit nil %>
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
<%= link_to 'Create', new_workout_path %>
|
||||
|
||||
<ul>
|
||||
<% @workouts.each do |workout| %>
|
||||
<li id="workout_<%= workout.id %>">
|
||||
<%= link_to workout.date, workout %> [
|
||||
<%= link_to workout.date, workout %> [
|
||||
<%= link_to 'Edit', edit_workout_path(workout) %> |
|
||||
<%= link_to 'Delete', workout_path(workout),
|
||||
data: { confirm: 'Are you sure?' }, :method => :delete %>
|
||||
]
|
||||
</li>
|
||||
<% end %>
|
||||
|
|
1
app/views/workouts/new.html.erb
Normal file
1
app/views/workouts/new.html.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<%= render :partial => 'form' %>
|
Loading…
Add table
Add a link
Reference in a new issue