删除嵌套对象

| 我有一个配置文件,此配置文件有很多
curso
(课程)。我会在个人资料的ѭ1上显示该个人资料中的所有课程。
<% for curso in @profile.cursos %>
<li><%=h curso.nome %> - <%=h curso.universidade %><br>
Ingresso em: <%=h curso.ano_ingresso %> - Encerra em: <%=h curso.ano_termino %> 
<li>
<%= button_to \'Delete\', { :action => \"destroy\", :id => curso.id },:confirm => \"Are you sure?\", :method => :delete %>
这样,我可以在个人资料页面上显示其个人资料中的所有课程,但是
button_to delete
无效。我已经尝试了很多事情,但是我觉得迷路了。关于如何创建链接或按钮或删除课程的任何想法?
已邀请:
在您的路线文件中
resources :profiles do
    resources :courses
end
然后,您可以只使用link_to方法
<%= link_to \"Delete\", profile_course_path(profile, course), :method => :delete %>
确保提供正确的变量
profile
course
然后,需要在courses_controller.rb中获取配置文件。
before_filter :get_profile

def get_profile
    @profile = Profile.find(params[:profile_id]) if params[:profile_id]
end

def destroy
  @course = Corse.find(params[:id])
  @course.destroy
  redirect_to profile_courses_path(@profile)
end 
这将带您使用嵌套课程返回正确的配置文件URL。 更新 对于新课程,您可以使用以下链接:
<%= link_to \"New Course\", new_profile_course_path(profile) %>
这将带您进入课程控制器中的“ 10”动作。 您应该在这里阅读嵌套表格。

要回复问题请先登录注册