0

과 다른 템플릿보기를 렌더링합니다. 그 중 두 가지는 중요한 것으로 보인다. 방법을 만들어 내레일 3.2.x 내가 코드의 추적 문자열을 시도하고 특정 ID

class Appointment < ActiveRecord::Base 
    attr_accessible :appointment_date, :appointment_notes, :appointment_time, :procedure_id, :patient_id 
    belongs_to :patient 
    belongs_to :procedure 

    validates :procedure_id, presence: true 
    validates :patient_id, presence: true 
    validates :appointment_date, presence: true 
    validates :appointment_time, presence: true 
class Patient < ActiveRecord::Base 
    attr_accessible :address1, :address2, :city, :comment, :email, :first_name, :init_date, :init_time, :last_name, :mobile, :notes, :phone, :state, :zip 
    before_validation :upcase_patient 
    before_save { self.email.downcase! } 
    has_many :appointments, dependent: :destroy 
    has_many :procedures, through: :appointments 

멋진 작동합니다. 그러나 데이터를 제출하고 약속에서 유효성 검사를 통과하지 않으면 정확한 app.dev/patients/ : id 페이지를 렌더링해야합니다. 여기서 : id는 현재 작업중인 페이지입니다. 문제의 양식은 (환자/쇼보기를 통해) 약속을 만드는 양식입니다. 잘못된 데이터 또는 무효 데이터가 제출되고 presence : true가 필요하면 동일한 페이지를 렌더링하고 싶습니다. 내가 현재받는 것은 :

RSpec에

ActionView::Template::Error: 
    undefined method `first_name' for nil:NilClass 
# ./app/views/patients/show.html.erb:1:in `_app_views_patients_show_html_erb__4137167421928365638_70201005779320' 
# ./app/controllers/appointments_controller.rb:11:in `create' 

나는이 특별히 그에 따라 옳은 길을 렌더링을 설정할 수있는 함께 할 권리 환자/ID를 호출 할 수있다 생각한다. 어떤 도움이라도 대단히 감사하겠습니다.

답변

1

가능성이 가장 큰 문제는 유효성 검사가 실패하고 템플릿을 렌더링 할 때 생성 작업에서 선언되지 않은 patients/show의 변수를 사용하고 있다는 것입니다. 이 문제를 해결하는 가장 좋은 방법은 당신이 많은 변수를 선언 할 때이 특히 건조하지 느낄 경우 유효성 검사가

def show 
    @patients = ... 
    @files = ... 
end 

def create 
    if @object_that_fails_validation.save 
    else 
    @patient = ... 
    @files = ... 
    #render the show action 
    end 
end 

를 실패 할 경우 생성 작용에 show 액션에 사용되는 변수의 동일한 세트를 선언하는 것입니다, 아약스를 통해 양식을 제출하거나 변수를 다른 방법으로 이동하십시오.

def show 
    set_variables 
end 

def create 
    if @object_that_fails_validation.save 
    else 
    set_variables 
    #render the show action 
    end 
end 

protected 

def set_variable 
    @patient = ... 
    @files = ... 
end 
+0

잘 작동합니다. 고맙습니다. 그러나 추천 한대로 변수를 '비공개'로 설정하고 '보호'하지 않았습니다. 이 모든 것이 잘 진행되었습니다! 건배! – sandovalg