嵌套资源,未定义方法 nilClass
Nested resources, undefined method nilClass
当我在 rails 4.2
中尝试在操作 'new' 上构建嵌套资源时出现错误 "undefined method "
这是我的路线:
devise_for :medics
resources :patients, shallow: true do
resources :consultations do
resources :prescriptions
end
end
我有用于系统登录的 Devise,我没有使用 "User" 作为模型名称,而是使用 "Medic" 以便使用注册表来设计创建一种医疗档案类型名称、phone 等新字段(我不知道是否是这里的问题)...
患者控制器:
class PatientsController < ApplicationController
before_action :set_medic
def new
@patient = @medic.patients.new
end
def create
@patient = @medic.patients.new(patient_params)
end
def set_medic
@medic = Medic.find_by_id(params[:medic_id])
end
型号:
class Medic < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :patients, :dependent => :destroy
end
class Patient < ActiveRecord::Base
belongs_to :medic, :foreign_key => :medic_id, :dependent => :destroy
has_many :consultations
accepts_nested_attributes_for :consultations
end
查看:
<%= link_to 'New Patient', new_patient_path(@medic) %>
rake 路线:
new_patient GET /patients/new(.:format) patients#new
错误:
nil:NilClass
的未定义方法“患者”
在这一行中:@patient = @medic.patients.new
有什么想法吗?提前致谢
因为你没有把 medic_id
放在你的路线中,你可能需要像这样在你的视图中澄清参数:
<%= link_to 'New Patient', new_patient_path(:medic_id => @medic.id) %>
问题很简单
您在每次请求时调用以下内容:
def set_medic
@medic = Medic.find_by_id(params[:medic_id])
end
问题是您没有通过 medic_id
路线:
devise_for :medics
resources :patients, shallow: true do #-> no medic_id here
resources :consultations do #-> or here
resources :prescriptions #-> or here
end
end
因此,发生的情况是您试图找到没有任何 id
的 Medic
,因此会出现 NilClass
错误。
您对 Rails 路由中的 nested resources
指令感到困惑:
#DON'T use this - it's just an example
#config/routes.rb
resources :medics do
resources :patients #-> url.com/medics/:medic_id/patients/:id
end
当您使用 Devise
时,我认为您可以围绕 current_medic
帮助程序确定您的调用范围(我认为您正在这样做)。 ..
-
修复
#app/controllers/patients_controller.rb
class PatientsController < ApplicationController
def new
@patient = current_medic.patients.new
end
def create
@patient = current_medic.patients.new(patient_params)
end
end
这样,您将能够使用(就像您正在使用 current_medic
):
<%= link_to "New", new_patient_path %>
当我在 rails 4.2
中尝试在操作 'new' 上构建嵌套资源时出现错误 "undefined method "这是我的路线:
devise_for :medics
resources :patients, shallow: true do
resources :consultations do
resources :prescriptions
end
end
我有用于系统登录的 Devise,我没有使用 "User" 作为模型名称,而是使用 "Medic" 以便使用注册表来设计创建一种医疗档案类型名称、phone 等新字段(我不知道是否是这里的问题)...
患者控制器:
class PatientsController < ApplicationController
before_action :set_medic
def new
@patient = @medic.patients.new
end
def create
@patient = @medic.patients.new(patient_params)
end
def set_medic
@medic = Medic.find_by_id(params[:medic_id])
end
型号:
class Medic < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :patients, :dependent => :destroy
end
class Patient < ActiveRecord::Base
belongs_to :medic, :foreign_key => :medic_id, :dependent => :destroy
has_many :consultations
accepts_nested_attributes_for :consultations
end
查看:
<%= link_to 'New Patient', new_patient_path(@medic) %>
rake 路线:
new_patient GET /patients/new(.:format) patients#new
错误:
nil:NilClass
的未定义方法“患者”在这一行中:@patient = @medic.patients.new
有什么想法吗?提前致谢
因为你没有把 medic_id
放在你的路线中,你可能需要像这样在你的视图中澄清参数:
<%= link_to 'New Patient', new_patient_path(:medic_id => @medic.id) %>
问题很简单
您在每次请求时调用以下内容:
def set_medic
@medic = Medic.find_by_id(params[:medic_id])
end
问题是您没有通过 medic_id
路线:
devise_for :medics
resources :patients, shallow: true do #-> no medic_id here
resources :consultations do #-> or here
resources :prescriptions #-> or here
end
end
因此,发生的情况是您试图找到没有任何 id
的 Medic
,因此会出现 NilClass
错误。
您对 Rails 路由中的 nested resources
指令感到困惑:
#DON'T use this - it's just an example
#config/routes.rb
resources :medics do
resources :patients #-> url.com/medics/:medic_id/patients/:id
end
当您使用 Devise
时,我认为您可以围绕 current_medic
帮助程序确定您的调用范围(我认为您正在这样做)。 ..
-
修复
#app/controllers/patients_controller.rb
class PatientsController < ApplicationController
def new
@patient = current_medic.patients.new
end
def create
@patient = current_medic.patients.new(patient_params)
end
end
这样,您将能够使用(就像您正在使用 current_medic
):
<%= link_to "New", new_patient_path %>