我有一个Rails应用程序,可让用户注册成为专业会员。如果他们想要取消它,他们可以点击此链接,这会触发promembers_controller.rb上的销毁操作
<%= link_to "Cancel my account", promember_path(@user), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => 'btn btn-mini' %>
我尝试在两个不同的页面上使用此链接,它只能从其中一个页面使用,即用户的个人资料页面
http://localhost:3000/lawyer_profiles/22-user-name
但是,我不希望该页面上的取消订阅链接。
如果我把链接放在嵌套的promembers_controller的'show'页面上,就像这样
http://localhost:3000/lawyer_profiles/22-user-name/promembers/40
或者如果我把它放在像这样的编辑页面上
http://localhost:3000/lawyer_profiles/22-user-name/promembers/37/edit
然后我收到此路由错误:
No route matches [DELETE] "/promembers"
我不明白为什么只要我有一条路线就可以在我放置链接的地方有所作为,我这样做
resources :promembers do
member { post :update_card }
end
我在promembers_controller.rb
可能需要注意的是,除了上面显示的非嵌套:promembers资源之外,我的routes.rb文件还有:promembers嵌套如下。
resources :lawyer_profiles do
resources :promembers
end
(我不确定嵌套和非嵌套的资源是否会影响链接行为。)
您能解释为什么链接无法在嵌套页面中运行吗?
答案 0 :(得分:1)
您正在使用路径助手构建链接:promember_path(@user)
并且正在为正在运行的页面的控制器操作中设置@user
变量。
在不起作用的页面上,您可能没有设置@user
变量,因此路径助手正在传递为nil。
确实你有[delete] / promembers /:id的路由,但你没有[delete] / promembers的路由,这是你将nil传递给helper时生成的。这会导致无路由错误。