2016-12-14 7 views
1

appointmentsのネストしたリソースがprofilesroute.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.erbにこのnew方法のためのフォームを持っているこの

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 

にリンクを得た理由を私は理解していませんここに私のせいをしてください。助けてください。

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

答えて

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は次のようになめらかでなければなりません。あなたはappointments_pathを持っていませんが、しかし、これはあなたがインスタンス変数@profileを作成する必要がどういう意味だけform_for

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

に両方のオブジェクトを渡し、そのパスを使用するにはprofile_appointments_path

を持っていますあなたのnew方法...

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

form_forは、既存のパスを使用しようとします。フォームで

関連する問題