用户在根URL下的配置文件URL

时间:2014-03-26 22:01:01

标签: ruby-on-rails rails-routing

用户的展示操作网址是“domain.com/users/(:id)”。如何将其更改为“domain.com /(:id)”?

(如果确实有任何不同,我会使用设计。)

我在路线中添加了这个,但它不起作用。

 get '(:username)' => 'users#show'

无法弄清楚如何在Rails路由guide中实现这一点。


更新

我将get "/:username" => 'users#show'放在路线文件的底部。当我浏览http://localhost:3000/testuser时,我会在日志中看到它:

Started GET "/testuser" for 127.0.0.1 at 2014-03-27 12:05:32 +0000
Processing by UsersController#show as HTML
  Parameters: {"username"=>"testuser"}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."username" IS NULL LIMIT 1
Redirected to http://localhost:3000/
Completed 302 Found in 5ms (ActiveRecord: 0.7ms)

testuser确实存在。但由于某种原因,nul并重定向到主页。如果我尝试转到http://localhost:3000/users/testuser,则用户的show动作会很好。

2 个答案:

答案 0 :(得分:1)

get "/:username" => 'users#show'

在root“some_path”

之上

<强>更新

路由文件从上到下匹配,比方说,你现在有一条路线

 get "/search" => "controller#method"

如果您在其前面加上“/:用户名”,那么您的应用可能会认为您正在尝试找到用户名为“搜索”的用户,因此请将“/:username”=&gt; “用户#show”位于路线列表的底部。

答案 1 :(得分:0)

我必须将路由get "/:id" => 'users#show'放在我的routes.rb文件