我有 Rails 路由问题。我想将单一资源与用户控制器一起使用,但它没有按我预期的那样工作。这是我的 routes.rb 文件的片段:
scope :module => "frontend" do
root :to => "home#index"
resource :user, :controller => "user"
get "/sign_up" => "user#new"
get "/sign_in" => "user#sign_in"
get "/sign_out" => "user#sign_out"
post "/authenticate" => "user#authenticate"
resources :articles
resources :article_categories
end
我认为当我使用例如“/user”或“/user/new”URL时它会起作用,但它没有。我收到路由错误:
No route matches {:controller=>"frontend/user"}
'rake routes' 命令输出为:
user POST /user(.:format) frontend/user#create
new_user GET /user/new(.:format) frontend/user#new
edit_user GET /user/edit(.:format) frontend/user#edit
GET /user(.:format) frontend/user#show
PUT /user(.:format) frontend/user#update
DELETE /user(.:format) frontend/user#destroy
sign_up GET /sign_up(.:format) frontend/user#new
sign_in GET /sign_in(.:format) frontend/user#sign_in
sign_out GET /sign_out(.:format) frontend/user#sign_out
验证 POST /authenticate(.:format) 前端/用户#authenticate
有趣的是,当我在用户控制器中为索引操作添加路由时,如下所示:
scope :module => "frontend" do
root :to => "home#index"
resource :user, :controller => "user"
get "/user" => "user#index"
get "/sign_up" => "user#new"
get "/sign_in" => "user#sign_in"
get "/sign_out" => "user#sign_out"
post "/authenticate" => "user#authenticate"
resources :articles
resources :article_categories
end
...有用!
但是用户控制器中没有定义索引操作!'rake routes' 命令为 GET /user 返回双行
GET /user(.:format) frontend/user#show
GET /user(.:format) frontend/user#index
所以我想这不是解决方案。分配给“/users”URL 的其他操作不起作用。
是否有必要为索引操作定义路由,例如
get "/controller_name" => "controller_name#index"
我究竟做错了什么?