簡體   English   中英

Rails路線尋找表演動作

[英]Rails routes looking for show action

我是Rails的新手,基本上我想制作一個用戶注冊表。

我目前正在使用Rails 3.2.3

基本上,我想在用戶的基本REST路由之上創建諸如user / register之類的基本路由。

這是我的routes.rb

resources :user do
  collection do
    get 'register'
  end
end

這是我的耙路:

     register_user_index GET    /user/register(.:format)   user#register
     user_index GET    /user(.:format)            user#index
                POST   /user(.:format)            user#create
       new_user GET    /user/new(.:format)        user#new
      edit_user GET    /user/:id/edit(.:format)   user#edit
           user GET    /user/:id(.:format)        user#show
                PUT    /user/:id(.:format)        user#update
                DELETE /user/:id(.:format)        user#destroy

據我了解,由於注冊用戶位於節目的頂部,因此在顯示之前,應先與注冊者匹配。

但是,當我嘗試執行localhost:3000 / user / register時,它給了我這個錯誤:

沒有路由匹配{:action =>“ show”,:controller =>“ user”}

有人可以幫我嗎?

謝謝!!

(我會對此發表評論,但我需要代碼示例的標記。)

我使用Rails 3.1.4嘗試了您的路線,它按預期運行。

我注意到您使用的是字符串而不是符號。 更標准的方法是:

resources :users do
  collection do
    get :register
  end
end

您是否拼錯資源? users通常是復數形式。 但這不是重點,這只是一個約定。 關鍵是您可能創建了一個名為UsersController的控制器,並且該控制器未遵循您的資源名稱。 選中它,或將其命名為UserController 但我強烈建議您要這樣做-如果您需要/user path,只需在資源中使用選項:path ,就可以了:

resources :users, path: 'user' do

同樣,如果還不算太晚,請使用:

resources :users do

您的注冊路徑將為/users/register

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM