Rails控制器中的自定义动作与平稳动作? [英] Custom actions in rails controller with restful actions?

查看:62
本文介绍了Rails控制器中的自定义动作与平稳动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用由脚手架生成的控制器中创建自己的动作时,我遇到了一些麻烦。

I'm having some trouble with using creating my own actions inside a controller I generated using the scaffold.

我了解所有内容都映射到了宁静的动作,但是我m建立一个用户控制器,用户可以在其中登录/注销等,但是当我创建动作并在route.rb中声明它时,访问用户/登录时会出现此错误

I understand everything maps to the restful actions but I'm building a user controller where users can login/logout, etc but when I created the action and declared it in the routes.rb I get this error when I visit users/login

Couldn't find User with id=login

It的用户尝试将登录名用作ID参数而不是将其用作操作。

It tries to use login as a ID parameter instead of using it as an action.

Routes.rb

Routes.rb

match 'users/login' => 'users#login'

我认为我在路线上做错了,所以如果有人可以帮忙我会很棒。

I think I'm doing something wrong with the routes so if anybody could help me that would be great.

谢谢

推荐答案

我认为您的 routes.rb 看起来像这样:

I assume your routes.rb looks like this:

resources :users
match 'users/login' => 'users#login'

问题是Rails使用了第一个匹配的路由。从文档

The problem is that Rails uses the first route that matches. From the documentation:


铁路路线按照指定的顺序进行匹配,因此如果 resources:photos 在<$ c上方$ c>获取照片/投票 显示操作行的路线将在 get 行之前匹配。要解决此问题,请将 get 行移到上方,将 resources 行匹配首先。

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action’s route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

因此要么在之前 resources:users定义您的自定义路由, code>:

So either define your custom route before resources :users:

match 'users/login' => 'users#login'
resources :users

…或将此语法用于添加更多RESTful行为

resources :users do
  collection do
    match 'login'
  end
end

要查看现有路线(及其顺序),请运行 耙子路线

To see the existing routes (and their order) run rake routes from the command line.

这篇关于Rails控制器中的自定义动作与平稳动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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