diff --git a/app/controllers/records_controller.rb b/app/controllers/records_controller.rb index a32d0b4..17d0363 100644 --- a/app/controllers/records_controller.rb +++ b/app/controllers/records_controller.rb @@ -1,15 +1,16 @@ class RecordsController < ApplicationController before_action :authenticate_user! before_action :set_record, only: [:edit, :update, :destroy] + before_action :set_records, only: [:index] + before_action :set_recent_records, only: [:new, :create] # GET /records def index - @records = current_user.records.order(target_date: :desc) end # GET /records/new def new - @record = current_user.records.build + @record = @records.build end # GET /records/1/edit @@ -18,7 +19,7 @@ def edit # POST /records def create - @record = current_user.records.build(record_params) + @record = @records.build(record_params) @record.subscribe(@service) if @record.save if current_user.update_second_step! @@ -48,6 +49,14 @@ def destroy private # Use callbacks to share common setup or constraints between actions. + def set_records + @records = current_user.records.order(target_date: :desc) + end + + def set_recent_records + @records = set_records.limit(5) + end + def set_record @record = current_user.records.find(params[:id]) end diff --git a/app/views/records/new.html.haml b/app/views/records/new.html.haml index 3b0fb06..71f279f 100644 --- a/app/views/records/new.html.haml +++ b/app/views/records/new.html.haml @@ -1,3 +1,19 @@ -%h1 記録する +.row + %h1 記録する + = render 'form' -= render 'form' +.row + %h1 最近の記録 + %table.table + %tr + %th 日付 + %th 体重 + %th コメント + %th 目標まで + + - @records.each do |record| + %tr + %td=l record.target_date + %td= show_weight record.weight + %td= record.comment + %td= "あと#{show_weight record.to_goal}kg"