Я пытаюсь создать app
, где практикующий может добавлять начальные дни и часы. Проблема сейчас в том, что когда я нажимаю save button
, он создает только одну запись. Должно быть создание записи для каждого дня недели.
Таким образом, это будет
понедельник: открыто 9.00 - закрыто 17.00 вторник: открыто 8.30 - закрыто 16.30 и c.
_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