【问题标题】:Match user routes with posts routes with and without resourcing将用户路线与帖子路线匹配(有资源和无资源)
【发布时间】:2013-03-14 07:18:32
【问题描述】:

我目前有一个路由文件

  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

【问题讨论】:

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


【解决方案1】:

你需要通过以下方式使用浅层路线,

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

【讨论】:

  • 我正在使用 rails 3.2.X 并且这样做了resources :users, :shallow =&gt; true do resources :problems end 并且仍然遇到相同的错误。
  • 能否列出用户控制器?
  • 我没有。我只是在使用设计给定的路线。我应该在用户控制器中使用什么样的方法
猜你喜欢
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多