我在appointments
的{{1}}中有一个嵌套资源profiles
。
route.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
的这种新方法。
AppointmentsController.rb
我不明白为什么每当我点击此链接时,我都会在网址中找到链接
<%= link_to "book a class", new_profile_appointment_path, class: 'btn-green2' %>
这是我需要的。但是有一个错误
http://localhost:3000/profiles/3/appointments/new
我无法在这里弄清楚我的错。请帮助。
答案 0 :(得分:1)
你表单中的网址应该是这样的:
<%= form_for [@profile, @appointment] do |f| %>
...
<% end %>
答案 1 :(得分: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
答案 2 :(得分: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