简体   繁体   中英

Ruby Adding to DB not working

I have problem because date_time doeasn't go to my database Could someone help me with this ? I go from New51 to Form51 and use create51 method. But still app creates empty date_time filed in db. Why?

Apppointment_controller:

class AppointmentsController < ApplicationController
  before_filter :load_appointment, only: [:show, :update, :edit, :destroy]
  before_filter :load_wizard, only: [:new, :edit, :create]



   def searchd
  end

    def move
    end

  def doctors_list
    @doctors = Doctor.where("imie_lekarza like ? or nazwisko_lekarza LIKE ? or specjalizacja LIKE ?", "%#{params[:search]}%", "%#{params[:search]}%", "%#{params[:search]}%")
  end

  def doctor_appointments
    @doctor = Doctor.find(params[:id])
    @appointments = @doctor.appointments
  end

  def change_appointment
    @appointment = Appointment.find(params[:id])
  end

  def change_doctor_and_appointment
    @doctors = Doctor.all
    @appointment = Appointment.find(params[:id])
  end

  def success
    @notice = flash[:notice]
  end

  def searchdate
    d = params[:date]
    data = Date.new(d["(1i)"].to_i, d["(2i)"].to_i, d["(3i)"].to_i)
    @appointments = Appointment.scoped 
    @appointments = @appointments.where(:data_godzina_wizyty => data.beginning_of_day..data.end_of_day)
  end


  def search_not
  end 

  def search_result_not

    #pacjent
    @patients = Patient.scoped
    @patients = @patients.where(pesel: params[:pesel])

    d = params[:date]
    if d["(1i)"] != "" and d["(2i)"]. != "" and d["(3i)"] != ""
        data = Date.new(d["(1i)"].to_i, d["(2i)"].to_i, d["(3i)"].to_i)
        @appointments = @patients.first.appointments.where(:data_godzina_wizyty => data.beginning_of_day..data.end_of_day)
    else
        @appointments = @patients.first.appointments
    end

  end

  def add_or_edit_not
    session['last_search_not'] = request.env["HTTP_REFERER"]
    @appointment = Appointment.find(params[:id])
    @patient = Patient.find(@appointment.patient_id)
    if @appointment.doctor_id != nil
        @doctor = Doctor.find(@appointment.doctor_id)
    end
    if @appointment.refferal_id != nil
        @refferal = Refferal.find(@appointment.refferal_id)
    end
  end

  def update_not
    @appointment = Appointment.find(params[:id])
    @appointment.notatka = params[:notatka]
    if @appointment.save
      redirect_to session[:last_search_not], notice: 'Notatka zostala zapisana.'
    else
      redirect_to :back, notice: 'Niestety wystapil blad. Prosze sprubowac pozniej'
    end
  end


  def search
  end


  def new51
     @appointment = Appointment.new

    respond_to do |format|
      format.html # new51.html.erb
      format.json { render json: @appointment }
    end
  end

  def create51
   @appointment = Appointment.new(params[:appointment])

    respond_to do |format|
      if @appointment.save
        format.html { redirect_to @appointment, notice: 'Wizyta zostala pomyslnie utworzona.' }
        format.json { render json: @appointment, status: :created, location: @appointment }
      else
        format.html { render action: "new51" }
        format.json { render json: @appointment.errors, status: :unprocessable_entity }
      end
    end
  end

  def search_result
    d = params[:date]
    data = Date.new(d["(1i)"].to_i, d["(2i)"].to_i, d["(3i)"].to_i)
  #szukanie pacjenta
    @patients = Patient.scoped
    @patients = @patients.where(pesel: params[:pesel])

  if params[:imie] != ""
      @patients = @patients.where(imie: params[:imie])
   end

   if params[:nazwisko] != ""
      @patients = @patients.where(nazwisko: params[:nazwisko])
   end
   #szukanie doctora
   opcja = 0
   @doctors = Doctor.scoped
  if params[:imie_lekarza] != ""
      @doctors = @doctors.where(imie_lekarza: params[:imie_lekarza])
      opcja = 1
   end

   if params[:nazwisko_lekarza] != ""
      @doctors = @doctors.where(nazwisko_lekarza: params[:nazwisko_lekarza])
      opcja = 1
   end
   #zlaczenie
    @patient_appo = @patients.first.appointments.where(:data_godzina_wizyty => data.beginning_of_day..data.end_of_day, potwierdzona: false)
   if opcja == 1
        @doctors_appo = @doctors.first.appointments.where(:data_godzina_wizyty => data.beginning_of_day..data.end_of_day, potwierdzona: false)
        @appointments = @patient_appo & @doctors_appo
    else
        @appointments = @patient_appo
   end
  end

  def to_confirm
    session['last_search'] = request.env["HTTP_REFERER"]
    @appointment = Appointment.find(params[:id])
    @patient = Patient.find(@appointment.patient_id)
    if @appointment.doctor_id != nil
        @doctor = Doctor.find(@appointment.doctor_id)
    end
    if @appointment.refferal_id != nil
        @refferal = Refferal.find(@appointment.refferal_id)
    end
  end

  def confirm
    @appointment = Appointment.find(params[:id])
    @appointment.potwierdzona = true
    if @appointment.save
      #redirect_to :back, notice: 'Rejestracja zostala pomyslnie potwierdzona.'
      redirect_to session[:last_search], notice: 'Rejestracja zostala pomyslnie potwierdzona.'
    else
      redirect_to :back, notice: 'Niestety wystapil blad. Prosze sprubowac pozniej'
    end
  end

  def index
    @appointments = Appointment.all
  end

  def show
  end

  def new
    @appointment = @wizard.object
    @clinics = Clinic.all
    @doctors = Doctor.all
  end

  public
  def findDoctorViaClinic( clinic )
    return( (Clinic.find(  clinic   )).doctors.uniq )
  end
  helper_method :findDoctorViaClinic

  def findScheduleViaDoctor(d)
    s = Schedule.includes(:doctors_workplace).where(doctors_workplace_id: (DoctorsWorkplace.includes(:doctor).where(doctor_id: d)) ).where(taken: 0) 
    return s
  end
    helper_method :findScheduleViaDoctor


  def edit
  end

  def create
    @appointment = @wizard.object
    if @wizard.save 
     s = ( Schedule.find( @appointment.schedule.id ) )
     s.taken = true
     s.save
      redirect_to @appointment, notice: "Appointment saved!"
    else
      render :new
    end
  end

  def update
#    if @wizard.save
#      redirect_to @appointment, notice: 'Appointment was successfully updated.'
#    else
#      render action: 'edit'
#    end

    @appointment = Appointment.find(params[:id])
    @old_appointment = @appointment.dup
    respond_to do |format|
      if @appointment.update_attributes(params[:appointment])
        DefaultMailer.move_appointment(@appointment, @old_appointment).deliver
        format.html { redirect_to appointments_success_path, notice: 'Pomyslnie zmieniono termin.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @appointment.errors, status: :unprocessable_entity }
      end
    end

  end

  def destroy
    @appointment.destroy
    redirect_to appointments_url
  end

private

  def load_appointment
    @appointment = Appointment.find(params[:id])
  end

  def load_wizard
    @wizard = ModelWizard.new(@appointment || Appointment, session, params)
    if self.action_name.in? %w[new edit]
      @wizard.start
    elsif self.action_name.in? %w[create update]
      @wizard.process
    end
  end
end

_form51.html.erb:

<%= form_for @appointment, :url => url_for(:action => "create51") do |f| %>
  <% if @appointment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@appointment.errors.count, "error") %> prohibited this appointment from being saved:</h2>

      <ul>
      <% @appointment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :data_godzina_wizyty %><br />
  <%=
    options = { start_year: 2.year.from_now.year,
                end_year: 2013,
                include_blank: true,
                default: nil }
    f.datetime_select :data_godzina_wizyty, options
  %>
  <!--<input type="text" data-behaviour='datepicker' :data_wizyty > -->
  </div>
  <div class="field">
    <%= f.hidden_field :doctor_id, :value => Doctor.find(session[:current_doctor_id2]).id %>
  </div>
  <div class="field">
    <%= f.hidden_field :patient_id, :value => Patient.find(session[:current_patient_id]).id %>
  </div>
  <div class="actions">
    <%= submit_tag "Utworz wizyte" %>
  </div>
<% end %>

new51.html.erb:

<div id="container">
<center>
<h1>Nowa wizyta:</h1>
<p>Sprawdz poprawnosc ponizszych danych a nastepnie uzupelnij formularz.</p>

<h3>Dane lekarza:</h3> 
<p>
  <strong>Imię lekarza:</strong>
   <%= Doctor.find(session[:current_doctor_id2]).imie_lekarza %>
</p>

<p>
  <strong>Nazwisko lekarza:</strong>
   <%= Doctor.find(session[:current_doctor_id2]).nazwisko_lekarza %>
</p>
<p>
  <strong>Specjalizacja lekarza:</strong>
   <%= Doctor.find(session[:current_doctor_id2]).specjalizacja %>
</p>
<h3>Dane pacjenta:</h3> 
<p>
  <strong>Imie:</strong>
   <%= Patient.find(session[:current_patient_id]).imie %>
</p>
<p>
  <strong>Nazwisko:</strong>
  <%= Patient.find(session[:current_patient_id]).nazwisko %>
</p>
<p>
  <strong>Pesel:</strong>
  <%= Patient.find(session[:current_patient_id]).pesel %>
</p>
<%= render "form51", locals: { appointment: @appointment } %>

<%= link_to 'Wybierz innego lekarza', index51_doctors_path %>
</br>
</center>
</div>

According to the comments above the problem was that you have used

@appointment = Appointment.new(params[:patient]) 

You are initialising your appointment with a params[:patient] but your params coming from form are more like "appointment"=>{} so instead you should do

@appointment = Appointment.new(params[:appointment])

This will initialise your appointment with the params coming from your form .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM