简体   繁体   English

路由稳定

[英]Restful Routing

I am writing a simple look up that checks to see if a username already exists in the database. 我正在编写一个简单的查询,以检查数据库中是否已经存在用户名。 So far things have been fairly straight forward. 到目前为止,事情一直很简单。 However I do have an issue with how I am doing my routing. 但是,我确实对路由方式有疑问。

Currently in my routes.rb I have the following: 当前在我的routes.rb中,我有以下内容:

namespace :broker do
    namespace :userdesk do
        resources :dashboards do
        end
        resources :accounts do      
            get :validateUsername
        end
    end
 end

When I use rake routes, I get the following route for validateUsername: broker_userdesk_account_validateUsername GET /broker/userdesk/accounts/:account_id/validateUsername(.:format) {:action=>"validateUsername", :controller=>"broker/userdesk/accoun ts"} 使用rake路由时,我将为validateUsername获得以下路由: broker_userdesk_account_validateUsername GET /broker/userdesk/accounts/:account_id/validateUsername(.:format) {:action=>"validateUsername", :controller=>"broker/userdesk/accoun ts"}

For this action, I am passing the username and not the account_id. 对于此操作,我要传递用户名而不是account_id。 Is there a way to modify the route so that it uses something like this: /broker/userdesk/accounts/:username/validateUsername(.:format) 有没有一种方法可以修改路由,使其使用如下/broker/userdesk/accounts/:username/validateUsername(.:format) :/ /broker/userdesk/accounts/:username/validateUsername(.:format) : /broker/userdesk/accounts/:username/validateUsername(.:format) : /broker/userdesk/accounts/:username/validateUsername(.:format)

First of all this doesn't seem particularly 'Railsy' or RESTful but you could define a specific route using match 首先,这似乎并不特别“ Railsy”或RESTful,但是您可以使用match定义特定的路由

match '/broker/userdesk/accounts/:username/validateUsername' => 'broker/userdesk/accounts#validateUsername'

UPDATED 更新

I recommend the rails guide on routing: http://guides.rubyonrails.org/routing.html . 我建议使用路由指南: http : //guides.rubyonrails.org/routing.html

I have ignored your namespaces in my examples. 我在示例中忽略了您的名称空间。

If you are nesting within the accounts resource, you would expect to scope by an account, not a username. 如果您要嵌套在帐户资源中,则希望按帐户而不是用户名进行范围设置。

It would be more RESTful to try something like /users/:id/validate . 尝试像/users/:id/validate这样的东西会更加RESTful。

In fact, if all you are doing is checking if the user exists, you could hit the show action of users and handle 404s: 实际上,如果您要做的只是检查用户是否存在,则可以执行用户的show操作并处理404:

resources :users # would give you /users/:id where you would either return a user or a not found (400)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM