我正在建立一个博客。我想将帖子关联到用户。我有这样的路线设置:
resources :users do
resources :posts
end
resources :sessions, :only => [:new, :create, :destroy]
match '/signup', :to => 'users#new'
match '/login', :to => 'sessions#new'
match '/logout', :to => 'sessions#destroy'
root :to => "pages#home"
这会将帖子与用户关联起来,当我rake routes
时,它看起来像这样:
user_posts GET /users/:user_id/posts(.:format) {:action=>"index", :controller=>"posts"}
user_posts POST /users/:user_id/posts(.:format) {:action=>"create", :controller=>"posts"}
new_user_post GET /users/:user_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_user_post GET /users/:user_id/posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
user_post GET /users/:user_id/posts/:id(.:format) {:action=>"show", :controller=>"posts"}
user_post PUT /users/:user_id/posts/:id(.:format) {:action=>"update", :controller=>"posts"}
user_post DELETE /users/:user_id/posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
users POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
user PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
user DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
sessions POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
/users/:user_id/posts/new(.:format) {:controller=>"posts", :action=>"create"}
/users/:user_id/posts/:id/edit(.:format) {:controller=>"posts", :action=>"update"}
/users/:user_id/posts/:id(.:format) {:controller=>"posts", :action=>"destroy"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
login /login(.:format) {:controller=>"sessions", :action=>"new"}
logout /logout(.:format) {:controller=>"sessions", :action=>"destroy"}
root /(.:format) {:controller=>"pages", :action=>"home"}
在我对路线进行任何其他更改之前,我会尝试创建新帖子,编辑和更新它们。但是,当我尝试创建,编辑或更新帖子时,我会收到错误:
No route matches "/users/1/posts/15/edit"
No route matches "/users/1/posts/new"
然后我玩了一下并将其添加到我的路线中:
match '/users/:user_id/posts/new', :to => 'posts#create'
match '/users/:user_id/posts/:id/edit', :to => 'posts#update'
match '/users/:user_id/posts/:id', :to => 'posts#destroy'
之后,一切正常。更新,创建,编辑等等但是我的一些测试失败了。
1) PostsController GET 'new' returns http success
Failure/Error: get 'posts#new'
ActionController::RoutingError:
No route matches {:controller=>"posts", :action=>"posts#new"}
# ./spec/controllers/posts_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
2) PostsController GET 'edit' returns http success
Failure/Error: get 'edit'
ActionController::RoutingError:
No route matches {:controller=>"posts", :action=>"edit"}
我知道使用这些路线一切正常,但是他们错了,我需要以其他方式路由它们吗?或者我可以在测试中改变哪些东西让它们通过?
感谢。
答案 0 :(得分:2)
get 'edit' => No route matches {:controller=>"posts", :action=>"edit"}
失败,因为该路线在参数中需要:user_id
。你想要:
get 'edit', :user_id => 1
顺便说一句,你似乎用match
语句覆盖了'足智多谋'的路线,例如:
match '/users/:user_id/posts/new', :to => 'posts#create'
覆盖new_user_post
语句创建的resources
路由。你确定那是什么意思吗?您通常希望/users/:user_id/posts/new
转到new
操作(呈现表单)而不是create
操作(处理已发布的表单)。