Ruby Adding To Database -
i new in ruby , have finish school project. have problem because create 1 project in 14 people, has own part. have create new appointment used before , cannot utilize 1 time again (i want add together simple form). please help me because stuck now, don't want alter someone's code. render _form51.html.erb new51.html.erb. maybe can utilize simple redirect create51?;> don't know how ;(
edited: solved why app create empty appointment?
routes.rb:
zoz::application.routes.draw resources :refferals collection 'new51' end fellow member 'show' end end resources :appointments collection 'search' #17 'search_result' #17 'to_confirm' #17 'search_not' #57 'search_result_not' #57 'add_or_edit_not' #57 'searchdate' 'searchd' 'move' 'new51' post :create51 end fellow member set :confirm #17 set :update_not #57 'show51' end end resources :clinics collection 'index51' end fellow member 'show51s' end end resources :doctors collection 'index51a' 'index51' 'search54' 'search_result54' 'show_harmonogram' end fellow member 'show51s' 'show51ss' end end resources :patients collection 'select51' 'index51' end fellow member 'show51s' 'show51ss' end end "welcome/index2" "welcome/index" 'appointments/create' 'appointments/move' => 'appointments#move' post 'appointments/move' => 'appointments#doctors_list' 'appointments/move/:id' => 'appointments#doctor_appointments', as: :doctor_appointments 'appointments/change_appointment/:id' => 'appointments#change_appointment', as: :change_appointment 'appointments/change_doctor_and_appointment/:id' => 'appointments#change_doctor_and_appointment', as: :change_doctor_and_appointment 'appointments/success' => 'appointments#success' # priority based upon order of creation: # first created -> highest priority. # sample of regular route: # match 'products/:id' => 'catalog#view' # maintain in mind can assign values other :controller , :action # sample of named route: # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase # route can invoked purchase_url(:id => product.id) # sample resource route (maps http verbs controller actions automatically): # sample resource route options: # resources :products # fellow member # 'short' # post 'toggle' # end # # collection # 'sold' # end # end # sample resource route sub-resources: # resources :products # resources :comments, :sales # resource :seller # end # sample resource route more complex sub-resources # resources :products # resources :comments # resources :sales # 'recent', :on => :collection # end # end # sample resource route within namespace: # namespace :admin # # directs /admin/products/* admin::productscontroller # # (app/controllers/admin/products_controller.rb) # resources :products # end # can have root of site routed "root" # remember delete public/index.html. root :to => 'welcome#index' # see how routes lay out "rake routes" # legacy wild controller route that's not recommended restful applications. # note: route create actions in every controller accessible via requests. # match ':controller(/:action(/:id))(.:format)' end _form51.html.erb:
<%= form_for(@appointment) |f| %> <% if @appointment.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@appointment.errors.count, "error") %> prohibited appointment beingness saved:</h2> <ul> <% @appointment.errors.full_messages.each |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 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' %> <%= link_to 'wybierz innego lekarza', index51_doctors_path %> </br> </center> </div> appointments_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 ? or nazwisko_lekarza ? or specjalizacja ?", "%#{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] info = 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)"] != "" , d["(2i)"]. != "" , d["(3i)"] != "" info = 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 |format| format.html # new.html.erb format.json { render json: @appointment } end end def create51 @appointment = appointment.new(params[:patient]) respond_to |format| if @appointment.save format.html { redirect_to @appointment, notice: 'szczegoy wizyty pomyslnie zmodyfikowane!' } format.json { render json: @appointment, status: :created, location: @appointment } else format.html { render action: "new" } format.json { render json: @appointment.errors, status: :unprocessable_entity } end end end def search_result d = params[:date] info = 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) homecoming 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 updated.' # else # render action: 'edit' # end @appointment = appointment.find(params[:id]) @old_appointment = @appointment.dup respond_to |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 logs:
started post "/appointments/create51" 127.0.0.1 @ 2014-06-22 08:22:53 +0200 processing appointmentscontroller#create51 html parameters: {"utf8"=>"✓", "authenticity_token"=>"kmxnerrh13opskupbg9hrm0sy5jvwdcabgrdnp5bsfc=", "appointment"=>{"data_godzina_wizyty(1i)"=>"2015", "data_godzina_wizyty(2i)"=>"1", "data_godzina_wizyty(3i)"=>"17", "data_godzina_wizyty(4i)"=>"16", "data_godzina_wizyty(5i)"=>"15", "doctor_id"=>"1", "patient_id"=>"1"}, "commit"=>"utworz wizyte"} [1m[35m (0.0ms)[0m begin transaction [1m[36msql (2.0ms)[0m [1minsert "appointments" ("clinic_id", "created_at", "data_godzina_wizyty", "data_wizyty", "doctor_id", "godzina_wizyty", "notatka", "objawy_choroby", "patient_id", "potwierdzona", "refferal_id", "schedule_id", "updated_at", "wymaga_potwierdzenia") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)[0m [["clinic_id", nil], ["created_at", sun, 22 jun 2014 06:22:53 utc +00:00], ["data_godzina_wizyty", nil], ["data_wizyty", nil], ["doctor_id", nil], ["godzina_wizyty", nil], ["notatka", nil], ["objawy_choroby", nil], ["patient_id", nil], ["potwierdzona", nil], ["refferal_id", nil], ["schedule_id", nil], ["updated_at", sun, 22 jun 2014 06:22:53 utc +00:00], ["wymaga_potwierdzenia", nil]] [1m[35m (13.0ms)[0m commit transaction redirected http://localhost:3000/appointments/30 completed 302 found in 23.0ms (activerecord: 15.0ms) started "/appointments/new51" 127.0.0.1 @ 2014-06-22 08:22:42 +0200 processing appointmentscontroller#new51 html [1m[35mdoctor load (1.0ms)[0m select "doctors".* "doctors" "doctors"."id" = ? limit 1 [["id", 1]] [1m[36mcache (0.0ms)[0m [1mselect "doctors".* "doctors" "doctors"."id" = ? limit 1[0m [["id", 1]] [1m[35mcache (0.0ms)[0m select "doctors".* "doctors" "doctors"."id" = ? limit 1 [["id", 1]] [1m[36mpatient load (0.0ms)[0m [1mselect "patients".* "patients" "patients"."id" = ? limit 1[0m [["id", 1]] [1m[35mcache (0.0ms)[0m select "patients".* "patients" "patients"."id" = ? limit 1 [["id", 1]] [1m[36mcache (0.0ms)[0m [1mselect "patients".* "patients" "patients"."id" = ? limit 1[0m [["id", 1]] [1m[35mcache (0.0ms)[0m select "doctors".* "doctors" "doctors"."id" = ? limit 1 [["id", 1]] [1m[36mcache (0.0ms)[0m [1mselect "patients".* "patients" "patients"."id" = ? limit 1[0m [["id", 1]] rendered appointments/_form51.html.erb (13.0ms) rendered appointments/new51.html.erb within layouts/application (22.0ms) rendered welcome/_form.html.erb (1.0ms) completed 200 ok in 103.0ms (views: 100.9ms | activerecord: 1.0ms) started "/appointments/30" 127.0.0.1 @ 2014-06-22 08:22:53 +0200 processing appointmentscontroller#show html parameters: {"id"=>"30"} [1m[36mappointment load (0.0ms)[0m [1mselect "appointments".* "appointments" "appointments"."id" = ? limit 1[0m [["id", "30"]] rendered appointments/show.html.erb within layouts/application (2.0ms) rendered welcome/_form.html.erb (1.0ms) completed 200 ok in 80.0ms (views: 77.0ms | activerecord: 0.0ms)
routes
wow routes wet
you really need read on resourceful routing - every route have in routes file needs associated particular controller (apart root to , other wildcards)
whoever wrote routes file has laden massive numbers of specific actions. frankly, it's mess , highly recommend go through & remove custom actions you've got in there.
resourceful routing described in rails docs thus:
bottom line shouldn't creating routes specific records; need create system handle different processes application have
--
form
if you're not seeing object created in db, there number of problems. biggest, though, utilize of @instance variable in partial.
to best of knowledge, partials don't carry @instance variables through render process. have pass local variables:
#new.html.erb <%= render "form51", locals: { appointment: @appointment } %> #_form51.html.erb <%= form_for appointment |f| %> ... <% end %> --
being honest, there's much prepare this, best if inquire help in comments here - can pinpoint needs fixed
ruby-on-rails ruby
No comments:
Post a Comment