Ruby on rails rails map.resources with有很多:通过dons';不行?
我有三个(相关)模型,具体如下:Ruby on rails rails map.resources with有很多:通过dons';不行?,ruby-on-rails,resources,map,has-many,Ruby On Rails,Resources,Map,Has Many,我有三个(相关)模型,具体如下: class User < ActiveRecord::Base has_many :posts has_many :comments has_many :comments_received, :through => :posts, :source => :comments end class Post < ActiveRecord::Base belongs_to :user has_many :comments e
class User < ActiveRecord::Base
has_many :posts
has_many :comments
has_many :comments_received, :through => :posts, :source => :comments
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
end
map.resources :users do |user|
user.resources :awards
user.resources :contest_entries do |contest_entry|
contest_entry.resources :awards
end
end
这应该能给我提供一些路线
user_posts_path
user_comments_path
user_comments_received_path
前两个有效,后一个无效。我在没有收到评论中的\uuu
的情况下尝试了它,但没有任何效果。我正在寻找一个像这样的URL
http://some_domain.com/users/123/comments_received
我也尝试过筑巢,但也许我做得不对。在这种情况下,我认为地图应该是:
map.resources :users do |user|
user.resources :comments
user.resources :posts, :has_many => :comments
end
然后url可能是:
http://some_domain.com/users/123/posts/comments
也许这是正确的方法,但我的语法错了
我是不是想错了?在我看来,我应该能够将所有评论添加到用户的所有帖子中,这似乎是合理的
谢谢你的帮助 我不得不承认,我对变量名有点困惑,但首先,我很惊讶您的代码有很多:从定义的角度来看都是通过工作的。模型的行为是否如您所期望的那样,将路线留出一秒钟
第二,这是变量名真正发挥作用的地方,路由对复数化有一些依赖性,因此您的foos条和baz可能是问题的原因,或者可能隐藏了问题。在任何情况下,你都可以这样写:
class User < ActiveRecord::Base
has_many :posts
has_many :comments
has_many :comments_received, :through => :posts, :source => :comments
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
end
map.resources :users do |user|
user.resources :awards
user.resources :contest_entries do |contest_entry|
contest_entry.resources :awards
end
end
我相信这会给你:
user_path, user_awards_path, user_contest_entry_path, and user_contest_entry_awards_path.
我不确定这是否真的回答了您的问题,如果您将foo、bar和baz更改为更接近真实情况的内容,这可能有助于更清楚地了解这里的情况。一个快速的解决方案是向您的用户控制器添加一个自定义方法(例如getusercomments),该方法将返回所有注释:
def getusercomments
@user = User.find(params[:id])
@comments = @user.posts.comments
end
然后将此方法添加到用户路由:
map.resources :users, :member => { :getusercomments => :get }
之后,您应该能够调用以下命令以获取用户的所有评论:
http://some_domain.com/users/123/getusercomments
尽管deinferesource和model关系的语法类似,但您不应该误以为资源映射到模型。读什么
您遇到的问题是生成的路由。使用嵌套语法,如下所示:
map.resources :users do |user|
user.resources :posts
user.resources :comments
user.resources :comments_received
end
然后运行“rake routes”
,给我(和其他东西一样)提供了:
因此,rails似乎正在将_索引添加到收到的评论路径的末尾。我承认我不知道为什么(与其他评论路线的冲突有关?),但这解释了你的问题
更好的选择可能是在您的评论资源上定义一个收集操作,如下所示:
map.resources :users do |user|
user.resources :posts
user.resources :comments, :collection => {:received => :get}
end
这将为您提供以下路线:
users GET /users {:action=>"index", :controller=>"users"}
user_posts GET /users/:user_id/posts {:action=>"index", :controller=>"posts"}
user_comments GET /users/:user_id/comments {:action=>"index", :controller=>"comments"}
received_user_comments GET /users/:user_id/comments/received {:action=>"received", :controller=>"comments"}
注意:收到的操作现在在评论控制器上好的,这不完全是我正在做的,但它足够近,可以称Foo为用户、Bar为Post、Baz为Comment,并且xxx被“接收”。目标是获得一条路线,显示给定用户对所有帖子的所有评论,注意用户不能对自己的帖子发表评论。