Reputation: 71
I'm trying to create an app
, where a practitioner can add opening days and hours. The problem right now is, that when I hit the save button
, it only creates one record. It should be creating a record for every day of the week.
So it would be
monday: open 9.00 - close 17.00 tuesday: open 8.30 - close 16.30 etc.
_form.html.erb
<script type="text/javascript">
$(document).ready(function () {
$('input:checkbox').bootstrapSwitch();
});
</script>
<%= @schedule.errors.full_messages %>
<%= simple_form_for([:clinic, :practitioner, @schedule]) do |f| %>
<table id="modal">
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<% Schedule.days.each do |day| %>
<tbody>
<tr>
<td id="modal-column-left" style="background-color:#aaa;">
<p>Åbningstider</p>
</td>
<td id="modal-column-right" style="background-color:#bbb;">
<p>Åbent/lukket</p>
</td>
</tr>
<tr>
<td id="modal-column-left" style="background-color:#aaa;">
<h2 style="margin-bottom: 5%;">
<h2 style="margin-bottom: 5%;"><%= day.titleize %>: <%= Date.today.send(day) %></h2>
<%= f.check_box :open_or_not, :data => { :size=>'small', 'on-color'=>'success', 'on-text'=>'YES', 'off-text'=>'NO' } %>
</td>
<td id="modal-column-right" style="background-color:#bbb;"><%= f.input :open_time %> <%= f.input :close_time %>
</td>
</tr>
</tbody>
<% end %>
</table>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
schedules_controller.rb
class SchedulesController < ApplicationController
before_action :set_schedule, only: [:show, :edit, :update, :destroy]
# GET /schedules
# GET /schedules.json
def index
@schedules = Schedule.all
end
# GET /schedules/1
# GET /schedules/1.json
def show
end
# GET /schedules/new
def new
@schedule = Schedule.new
@schedules = Schedule.all
end
# GET /schedules/1/edit
def edit
end
# POST /schedules
# POST /schedules.json
def create
@schedule = Schedule.new(schedule_params)
respond_to do |format|
if @schedule.save
format.html { redirect_to clinic_practitioner_schedule_path(id: @schedule.id), notice: 'Schedule was successfully created.' }
format.json { render :show, status: :created, location: @schedule }
else
format.html { render :new }
format.json { render json: @schedule.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schedules/1
# PATCH/PUT /schedules/1.json
def update
respond_to do |format|
if @schedule.update(schedule_params)
format.html { redirect_to clinic_practitioner_schedule_path(@schedule), notice: 'Schedule was successfully updated.' }
format.json { render :show, status: :ok, location: @schedule }
else
format.html { render :edit }
format.json { render json: @schedule.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schedules/1
# DELETE /schedules/1.json
def destroy
@schedule.destroy
respond_to do |format|
format.html { redirect_to clinic_practitioner_schedules_url, notice: 'Schedule was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_schedule
@schedule = Schedule.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def schedule_params
params.require(:schedule).permit(:title, :start, :end, :practitioner_id, :account_id, :open_or_not, :day_of_week, :open_time, :close_time)
end
end
Upvotes: 0
Views: 194
Reputation: 5061
Read the documentation of complex forms.
You will want to attach multiple schedules to the Practitioner
:
class Practitioner < ApplicationRecord
has_many :schedules
accepts_nested_attributes_for :schedules
end
The Controller
def practitioner_params
params.require(:practitioner).permit(:name, schedule_attributes: [:id, :open_or_not, :day_of_week, :open_time, :close_time])
end
And in the form:
<%= simple_form_for @practitioner do |f| %>
<%= f.input :name %>
<%= f.simple_fields_for :schedules do |s| %>
<%= s.input :open_or_not %>
<%= s.input :day_of_week %>
<%= s.input :open_time %>
<%= s.input :close_time %>
<% end %>
<% end %>
To dynamically add rows use the Cocoon gem.
Upvotes: 1