删除嵌套对象

发布于 2024-11-02 01:33:53 字数 594 浏览 1 评论 0原文

我有一个个人资料,该个人资料有许多 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 cursos (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2024-11-09 01:33:53

在你的路由文件中

resources :profiles do
    resources :courses
end

然后你可以使用link_to方法

<%= link_to "Delete", profile_course_path(profile, course), :method => :delete %>

确保你提供了正确的变量profilecourse

然后在你的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 

这将带您返回到正确的个人资料网址及其嵌套课程。

更新

对于新课程,您可以使用以下链接:

<%= link_to "New Course", new_profile_course_path(profile) %>

这将带您进入课程控制器中的new 操作。

您应该在此处阅读嵌套表单。

In your routes file

resources :profiles do
    resources :courses
end

Then you can just use link_to method

<%= link_to "Delete", profile_course_path(profile, course), :method => :delete %>

Make sure you are providing the correct variables profile and course

Then in your courses_controller.rb you need to get the profile.

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 

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:

<%= link_to "New Course", new_profile_course_path(profile) %>

That will take you to the new action in the courses controller.

You should read up on nested forms here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文