2016-12-14 64 views
1

我有一個嵌套的資源appointmentsprofilesroute.rb未定義的方法約會路徑使用嵌套的資源

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

在我AppointmentsController.rb我寫這樣

def new 
    @appointment = Appointment.new 
    end  

新方法,我對這個new方法形式appointments/new.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> 

我有一個鏈接標籤在profiles/show.html.erb爲callling新方法AppointmentsController.rb

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

我不明白爲什麼每當我點擊這個鏈接我在URL

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

這是我need.But有錯誤 undefined method 'appointments_path' for #<#<Class:0x007f24ccc16bf8>:0x007f24ccc154b0>

我想不出有鏈接我的錯在這裏,請幫忙。

回答

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

地址在你的形式應該是水木清華這樣的:

<%= 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| %> 

但是,這並不意味着你需要創建一個實例變量@profilenew方法...

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