Ruby添加到DB无法正常工作

时间:2014-06-22 14:19:50

标签: ruby-on-rails ruby

我有问题,因为date_time不能进入我的数据库有人可以帮我吗?我从New51转到Form51并使用create51方法。但仍然应用程序创建db中的空date_time。为什么呢?

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>

1 个答案:

答案 0 :(得分:1)

根据上述评论,问题在于您使用过

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

使用params [:patient] 初始化您的约会 但是来自表单的params更像"appointment"=>{}所以相反,您应该 p>

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

这将使用来自您表单的参数初始化您的约会。