I'm working on a "fitness" tracker for users to enter things like "weight, Mile Time". I'm very novice with RoR and I'm trying to write my form so that if a user makes a "New" stats post, it will default the last data from the database rather than just empty values if they don't include all new values. let me elaborate: If a user clicks "New" they can enter their weight and mile time. If they don't include both, I get an empty value in my table. I believe this can be accomplished though hidden_fields but I'm unsure how to use them - even after reading documentation.
<% @stats.each do |stat| %>
<table class="table table-sm">
<thead>
<th scope="col">Time</th>
<th scope="col">Date</th>
<th scope="col"></th>
</thead>
<tbody>
<tr>
<td><%= stat.mile %></td>
<td><%= stat.created_at.strftime("#{stat.created_at.day.ordinalize} %B %Y") %></td>
<td><%= link_to 'Edit', edit_stat_path(stat), class: "btn btn-secondary btn-sm" %>
<%= link_to 'Delete', stat, method: :delete, class: "btn btn-danger btn-sm", data: { confirm: 'Are you sure?' } %></td>
</tr>
</table>
<% end %>
<% @stats.each do |stat| %>
<table class="table table-sm">
<thead>
<th scope="col">Weight</th>
<th scope="col">Date</th>
<th scope="col"></th>
</thead>
<tbody>
<tr>
<td><%= stat.weight %></td>
<td><%= stat.created_at.strftime("#{stat.created_at.day.ordinalize} %B %Y") %></td>
<td><%= link_to 'Edit', edit_stat_path(stat), class: "btn btn-secondary btn-sm" %>
<%= link_to 'Delete', stat, method: :delete, class: "btn btn-danger btn-sm", data: { confirm: 'Are you sure?' } %></td>
</tr>
</table>
<% end %>
Here is my very basic form for new stats:
<%= form_with(model: stat) do |form| %>
<% if stat.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(stat.errors.count, "error") %> prohibited this stat from being saved:</h2>
<ul>
<% stat.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :mile %>
<%= form.text_field :mile %>
</div>
<div class="field">
<%= form.label :weight %>
<%= form.text_field :weight %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Any advice or help would be greatly appreciated!
EDIT Added stats_controller
**stats_controller.rb**
class StatsController < ApplicationController
before_action :authenticate_user!
before_action :set_stat, only: [:show, :edit, :update, :destroy]
# GET /stats
def index
@stats = current_user.stats.order(created_at: :desc).page(params[:page]).per_page(5)
end
# GET /stats/1
def show
end
# GET /stats/new
def new
@stat = current_user.stats.new
end
# GET /stats/1/edit
def edit
end
# POST /stats
def create
@stat = current_user.stats.new(stat_params)
if @stat.save
redirect_to @stat, notice: 'Stat was successfully created.'
else
render :new
end
end
# PATCH/PUT /stats/1
def update
if @stat.update(stat_params)
redirect_to @stat, notice: 'Stat was successfully updated.'
else
render :edit
end
end
# DELETE /stats/1
def destroy
@stat.destroy
redirect_to stats_url, notice: 'Stat was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_stat
@stat = Stat.find(params[:id])
end
# Only allow a list of trusted parameters through.
def stat_params
params.require(:stat).permit(:user_id, :mile, :weight, :waist, :hips, :leftleg, :rightleg, :leftarm, :rightarm, :chest,:bust,:wallsit, :plank, :pushup, :burpee, :situp, :jumpsquat)
end
end
CodePudding user response:
you should do this in your controller code...
since you didn't include controller code, you can also do this in your view:
<%
last_stat = current_user.stats.where.not(weight: nil).last
stat.weight = last_stat.weight
stat.mile = last_stat.mile
%>
<%= form_with(model: stat) do |form| %>
# ...
Inside your form inputs, you can also add required: true
so the user is required to submit them...