2016-12-14 1 views
1

appointmentsprofiles에있는 route.rb에 중첩 리소스가 있습니다. 내 AppointmentsController.rb에서중첩 된 리소스를 사용하는 'appointments_path'정의되지 않은 메소드

resources :profiles, only: [:show, :update, :edit] do 
resources :appointments 
end 

나는이 새를 callling 위해 내가 profiles/show.html.erb에서 링크 태그가 나는

<section class="login-page border-top-blue padding-v-10"> 
    <section class="login-details-div bg-light-grey"> 

    <%= form_for(@appointment, :html => { :class => 'form-horizontal login-form-container login-form-div' }) do |f| %> 

    <h1>Create New Appointments</h1> 

    <div class="form-div-send-for-inlinement-icon-with-text-field"> 
    <span class="form-wrapper "> 
     <i class="fa fa-user login-icon-white login-icon-envelop "></i> 
    </span> 
     <div class="label-input-div input-width"> 
     <label for="" class="login-label-div"></label> 
     <div class="form-wrapper"><%= f.text_area :description, class:'login-icon-white', autofocus: true %></div> 
     </div> 

    </div> 

    <div class="form-div-send-for-inlinement-icon-with-text-field"> 
    <span class="form-wrapper "> 
     <i class="fa fa-envelope login-icon-white login-icon-envelop "></i> 
    </span> 
     <div class="label-input-div input-width"> 
     <label for="" class="login-label-div">topic</label> 
     <div class="form-wrapper"><%= f.text_field :topic, class:'login-icon-white' %></div> 
     </div> 

    </div> 

    <div class="form-div-send-for-inlinement-icon-with-text-field"> 
    <span class="form-wrapper "> 
     <i class="fa fa-envelope login-icon-white login-icon-envelop "></i> 
    </span> 
     <div class="label-input-div input-width"> 
     <label for="" class="login-label-div">Appointment Date</label> 
     <div class="form-wrapper"><%= f.date_field :appointment_date, class:'login-icon-white' %></div> 
     </div> 

    </div> 

    <div class="form-div-send-for-inlinement-icon-with-text-field"> 
    <span class="form-wrapper "> 
     <i class="fa fa-envelope login-icon-white login-icon-envelop "></i> 
    </span> 
     <div class="label-input-div input-width"> 
     <label for="" class="login-label-div">class hours</label> 
     <div class="form-wrapper"><%= f.number_field :class_hours, class:'login-icon-white' %></div> 
     </div> 

    </div> 



    <div class="form-group"> 
     <label for="" class="col-sm-offset-2 col-sm-10 col-xs-offset-2 col-xs-10 col-md-offset-1 col-md-11 login-btn-div"> 

     <%= f.submit "submit", :class => "login-btn" %> 
     </label> 
     <label for="" class="col-sm-offset-2 col-sm-10 col-xs-offset-2 col-xs-10 col-md-offset-1 col-md-11 login-btn-div"> 

     <%= link_to "Cancel", root_path(), :class => "login-btn" %> 
     </label> 
    </div> 

    <% end %> 
    </section> 
    </section> 

appointments/new.html.erbnew 방법에 대한 양식을 가지고이

def new 
    @appointment = Appointment.new 
    end  

같은 새로운 방법을 쓰기 방법은 AppointmentsController.rb입니다.

 <%= link_to "book a class", new_profile_appointment_path, class: 'btn-green2' %> 

나는이 링크를 클릭 할 때마다 나는 내가 이해할 수 없었다 오류 undefined method 'appointments_path' for #<#<Class:0x007f24ccc16bf8>:0x007f24ccc154b0>

이 need.But입니다 URL

http://localhost:3000/profiles/3/appointments/new 

에 링크를 가지고 왜 이해가 안 돼요 여기 내 잘못을. 제발 도와 줘. 양식에

답변

1

당신이 당신의 약속이 중첩 될 아래 프로필을 필요로 모든 약속을 만들기위한 중첩 된 자원을 사용하는 것처럼. 따라서 profile_id을 전달해야하며 귀하의 약속은 귀하의 경로로 작성됩니다. new_profile_appointment_path도 있습니다.

<%= form_for([@profile, @appointment], :html => { :class => 'form-horizontal login-form-container login-form-div' }) do |f| %> 

또한 방법에 @profile을 정의해야합니다.

def new 
    @profile = Profile.find(params[:profile_id]) 
    @appointment = Appointment.new 
    end 
1

URL은 다음과 떨어지게해야한다 :

<%= form_for [@profile, @appointment] do |f| %> 
    ... 
<% end %> 
1

form_for 시도는 기존의 경로를 사용 할 수 있습니다. 당신은 appointments_path이없는하지만 당신은이 profile_appointments_path

단지 그러나 form_for

<%= form_for([@profile, @appointment], :html => { :class => 'form-horizontal login-form-container login-form-div' }) do |f| %> 

에 오브젝트를 모두 통과하는 경로를 사용하려면, 이것은 당신이 변수 @profile을 인스턴스를 만들 필요가 의미 하는가해야합니까 당신의 new 방법 ...

def new 
    @profile = Profile.find(params[:profile_id]) 
    @appointment = Appointment.new 
    end 
관련 문제