将用户路线与包含和不包含资源的帖子路线相匹配

时间:2013-03-14 07:18:33

标签: ruby-on-rails-3 activerecord devise

我目前有一个路径文件

  devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
  resources :users  do
  resources :posts
end
  match "posts" => "posts#main" 
  get "home/index"
  root :to => 'home#index'
end

我现在想做的是,/users/:user_id/problems/:id显示为/problems/:id。但不确定我应该如何将它放入路线?

注意:我确实在用户和帖子之间建立了关系。

class User < ActiveRecord::Base
 has_many :posts

class Post < ActiveRecord::Base
  belongs_to :user

由于

UDPATE:我得到的错误是Couldn't find User without an ID

1 个答案:

答案 0 :(得分:0)

您需要以下列方式使用浅路线,

 resources :users, :shallow => true do |user|
  user.resources :posts
 end