删除嵌套对象
我有一个个人资料,该个人资料有许多 curso
(课程)。我在该个人资料的 show.html.erb
上显示该个人资料的所有课程。
<% 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
不起作用。我已经尝试过很多事情,但我想我迷失了。知道如何创建链接或按钮或任何内容来删除课程吗?
I have a profile, and this profile has many curso
s (courses). I show all the courses a profile has on the show.html.erb
of this profile.
<% 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 %>
This way, I'm able to show all the courses a profile has on it's page, but the button_to delete
just doesn't work. I have tried many things already, but I think I'm lost. Any idea on how I can create a link or a button or anything to delete the courses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的路由文件中
然后你可以使用link_to方法
确保你提供了正确的变量
profile
和course
然后在你的courses_controller.rb中你需要获取配置文件。
这将带您返回到正确的个人资料网址及其嵌套课程。
更新
对于新课程,您可以使用以下链接:
这将带您进入课程控制器中的
new
操作。您应该在此处阅读嵌套表单。
In your routes file
Then you can just use link_to method
Make sure you are providing the correct variables
profile
andcourse
Then in your courses_controller.rb you need to get the profile.
That will take you back to the correct profile url with it's nested courses.
update
For new courses you can use the following link:
That will take you to the
new
action in the courses controller.You should read up on nested forms here.