在Rails中轻松布线 [英] Simple routing in Rails

查看:70
本文介绍了在Rails中轻松布线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从用户显示视图中破坏链接。

I'm trying to destroy a link from a User Show view.

因此来自 / users / 1

我想在 teacher_student_links 破坏操作

I want to access destroy action in teacher_student_links controller.

第一次尝试:

<%= link_to 'destroy', :controller => "teacher_student_links", :action => "destroy", :id => @user.id %>

此操作失败,因为它会路由到指定控制器中的显示操作。 第一个问题:为什么它指向显示操作而不是破坏?我试图用括号将上面的内容括起来,并且也得到了毫无意义的错误。

This fails because it routes to "show" action in the specified controller. So first question: why does it point to the "show" action instead of "destroy?" I tried to enclose the above with parenthesis, and got a meaningless error as well.

第二次尝试:

config / routes.rb

match '/bye_teacher' => 'teacher_student_links#destroy'

视图

<%= link_to 'destroy', '/bye_teacher', :user_id => @user.id %>

也尝试过,

<%= link_to 'destroy', '/bye_teacher', :user_id => @user %>

两行正确地直接指向指定的控制器和操作,但是未传入参数。(可以'找不到没有ID错误的用户)

Both lines correctly direct to the specified controller and action, but the parameter is not passed in. (can't find the user without an ID error)

第二个问题:在这种情况下,我是否以错误的方式传递了变量?

问这两个问题有点尴尬,但我想知道这些问题背后的原因。

It's little embarrassing to ask these two questions, but I wanted to know the reasons behind these problems.

更新:

<%= link_to 'destroy', teacher_student_links_path(:user_id => @user), :method => :delete %>

给予

没有路线匹配[删除] / teacher_student_links

No route matches [DELETE] "/teacher_student_links"

<%= link_to 'destroy', teacher_student_links_path(@user), :method => :delete %>

给予

No route matches [DELETE] "/teacher_student_links.1"

...所以我跑

耙路,我得到了

删除/teacher_student_links/:id(.:format)Teacher_student_links#destroy

推荐答案

您输入的路径错误

<%= link_to 'destroy', teacher_student_links_path(@user), :method => :delete %>

应该是这样的:

<%= link_to 'destroy', teacher_student_link_path(@user), :method => :delete %>

当您运行耙路时,第一栏会告诉您路径

When you run 'rake routes' then 1st column will tell you the path

这篇关于在Rails中轻松布线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆