这让我很疯狂。我不知道我做错了什么,我收到了这个错误:
ActiveRecord::RecordNotFound in AppointmentsController#new
Couldn't find Schedule without an ID
app/controllers/appointments_controller.rb:25:in `new'
我确信这是一件简单的事,但我看不到它。有人可以指出我的问题以及如何解决它?
这是我的代码
的routes.rb
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
root :to => "Doctors#index"
resources :specialties
resources :branches
resources :doctors do
resources :appointments
resources :schedules
end
end
match '*path', to: redirect("/#{I18n.default_locale}/%{path}")
match '', to: redirect("/#{I18n.default_locale}")
关系
Class Appointment ...
belongs_to :doctor
belongs_to :schedule
end
Class Schedule ...
belongs_to :doctor
has_many :appointments
end
Class Doctor ...
has_many :appointments, dependent: :destroy
has_many :schedules, dependent: :destroy
end
模式
create_table "appointments", :force => true do |t|
t.integer "doctor_id"
t.date "adate"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.time "atime"
t.string "personal_id"
t.string "insurance_id"
t.string "prescription"
t.string "email"
t.string "full_name"
t.integer "schedule_id"
end
create_table "doctors", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "schedule"
t.string "prefix"
end
create_table "schedules", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "doctor_id"
t.boolean "sun"
t.boolean "mon"
t.boolean "tue"
t.boolean "wed"
t.boolean "thu"
t.boolean "fri"
t.boolean "sat"
end
控制器
def new
@schedule = Schedule.find(params[:schedule_id])
@doctor = Doctor.find(params[:doctor_id])
@appointment = @doctor.appointments.new
end
_form
<h1><%= @doctor.name %></h1>
<%= simple_form_for([@doctor, @appointment], :html => { :multipart => true, :class => 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<%= f.input :full_name %>
<%= f.input :email %>
<%= f.input :adate, as: :date_picker %>
<%= f.input :atime, as: :time_picker %>
<%= f.input :personal_id %>
<%= f.input :insurance_id %>
<%= f.input :prescription %>
<div class="controls"><%= f.button :submit, class: "btn btn-primary" %></div>
<% end %>
rails console test:
Loading development environment (Rails 3.2.11)
1.9.3p125 :001 > s = Schedule.last
Schedule Load (4.2ms) SELECT "schedules".* FROM "schedules" ORDER BY "schedules"."id" DESC LIMIT 1
=> #<Schedule id: 2, created_at: "2013-01-23 08:40:14", updated_at: "2013-01-23 08:40:14", doctor_id: 1, sun: true, mon: true, tue: true, wed: true, thu: true, fri: true, sat: true>
1.9.3p125 :002 > s.appointments
Appointment Load (0.7ms) SELECT "appointments".* FROM "appointments" WHERE "appointments"."schedule_id" = 2
=> []
root /:locale(.:format) Doctors#index
specialties GET /:locale/specialties(.:format) specialties#index
POST /:locale/specialties(.:format) specialties#create
new_specialty GET /:locale/specialties/new(.:format) specialties#new
edit_specialty GET /:locale/specialties/:id/edit(.:format) specialties#edit
specialty GET /:locale/specialties/:id(.:format) specialties#show
PUT /:locale/specialties/:id(.:format) specialties#update
DELETE /:locale/specialties/:id(.:format) specialties#destroy
branches GET /:locale/branches(.:format) branches#index
POST /:locale/branches(.:format) branches#create
new_branch GET /:locale/branches/new(.:format) branches#new
edit_branch GET /:locale/branches/:id/edit(.:format) branches#edit
branch GET /:locale/branches/:id(.:format) branches#show
PUT /:locale/branches/:id(.:format) branches#update
DELETE /:locale/branches/:id(.:format) branches#destroy
doctor_appointments GET /:locale/doctors/:doctor_id/appointments(.:format) appointments#index
POST /:locale/doctors/:doctor_id/appointments(.:format) appointments#create
new_doctor_appointment GET /:locale/doctors/:doctor_id/appointments/new(.:format) appointments#new
edit_doctor_appointment GET /:locale/doctors/:doctor_id/appointments/:id/edit(.:format) appointments#edit
doctor_appointment GET /:locale/doctors/:doctor_id/appointments/:id(.:format) appointments#show
PUT /:locale/doctors/:doctor_id/appointments/:id(.:format) appointments#update
DELETE /:locale/doctors/:doctor_id/appointments/:id(.:format) appointments#destroy
doctor_schedules GET /:locale/doctors/:doctor_id/schedules(.:format) schedules#index
POST /:locale/doctors/:doctor_id/schedules(.:format) schedules#create
new_doctor_schedule GET /:locale/doctors/:doctor_id/schedules/new(.:format) schedules#new
edit_doctor_schedule GET /:locale/doctors/:doctor_id/schedules/:id/edit(.:format) schedules#edit
doctor_schedule GET /:locale/doctors/:doctor_id/schedules/:id(.:format) schedules#show
PUT /:locale/doctors/:doctor_id/schedules/:id(.:format) schedules#update
DELETE /:locale/doctors/:doctor_id/schedules/:id(.:format) schedules#destroy
doctors GET /:locale/doctors(.:format) doctors#index
POST /:locale/doctors(.:format) doctors#create
new_doctor GET /:locale/doctors/new(.:format) doctors#new
edit_doctor GET /:locale/doctors/:id/edit(.:format) doctors#edit
doctor GET /:locale/doctors/:id(.:format) doctors#show
PUT /:locale/doctors/:id(.:format) doctors#update
DELETE /:locale/doctors/:id(.:format) doctors#destroy
/*path(.:format) :controller#:action
/ :controller#:action
appointment_steps GET /appointment_steps(.:format) appointment_steps#index
POST /appointment_steps(.:format) appointment_steps#create
new_appointment_step GET /appointment_steps/new(.:format) appointment_steps#new
edit_appointment_step GET /appointment_steps/:id/edit(.:format) appointment_steps#edit
appointment_step GET /appointment_steps/:id(.:format) appointment_steps#show
PUT /appointment_steps/:id(.:format) appointment_steps#update
DELETE /appointment_steps/:id(.:format) appointment_steps#destroy