我在Ruby on Rails中遇到了路由问题。我以这种方式配置了路线
resources :users do
collection do
resource :registrations, only: [:show, :create]
resource :sessions, only: [:new, :create, :destroy]
resource :confirmations, only: [:show]
end
end
我有一个RegistrationsController,我有两个端点(new,create)
class RegistrationsController < ApplicationController
skip_before_filter :authenticate!
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = t("registrations.user.success")
redirect_to :root
end
end
end
但是当我做rails并且我把localhost:3000 / users / registrations / create或new我得到了#34;没有路由匹配&#34;。而且我认为这条路线存在,因为如果我做耙路线,我就会得到这个
registrations POST /users/registrations(.:format) registrations#create
GET /users/registrations(.:format) registrations#show
我知道这应该是一个愚蠢的错误,但我不明白。我感谢任何帮助
答案 0 :(得分:1)
定义注册路线时,您只需将其限制为[:show, :create]
:
resource :registrations, only: [:show, :create]
但你的控制器(正确!)假设有两条路线:new
(显示注册表格)和create
(创建新用户)。您需要更改路线,使其与控制器操作匹配:
resources :users do
collection do
resource :registrations, only: [:new, :create] # Updated this line!
resource :sessions, only: [:new, :create, :destroy]
resource :confirmations, only: [:show]
end
end