如何生成合适的嵌套资源url_for? [英] How to generate the proper `url_for` a nested resource?
问题描述
我正在使用Ruby on Rails 3.2.2,并且想为嵌套资源生成一个正确的 url_for
URL。也就是说,我有:
I am using Ruby on Rails 3.2.2 and I would like to generate a proper url_for
URL for a nested resource. That is, I have:
# config/routes.rb
resources :articles do
resources :user_associations
end
# app/models/article.rb
class Article < ActiveRecord::Base
...
end
# app/models/articles/user_association.rb
class Articles::UserAssociation < ActiveRecord::Base
...
end
注意:生成的命名路由就像 article_user_association
, article_user_association
, edit_article_user_association
,...
Note: generated named routes are like article_user_associations
, article_user_association
, edit_article_user_association
, ...
在我看来,我使用:
url_for([@article, @article_association])
然后我得到以下错误:
NoMethodError
undefined method `article_articles_user_association_path' for #<#<Class:0x000...>
但是,如果我以这种方式声明路由器
However, if I state routers this way
# config/routes.rb
resources :articles do
resources :user_associations, :as => :articles_user_associations
end
url_for
方法会按预期工作,并且会生成例如URL / articles / 1 / user_associations / 1
。
the url_for
method works as expected and it generates, for instance, the URL /articles/1/user_associations/1
.
注意:在这种情况下,生成的命名路由类似于 article_articles_user_associations
, article_articles_user_association
, edit_article_articles_user_association
,...
Note: in this case, generated named routes are like article_articles_user_associations
, article_articles_user_association
, edit_article_articles_user_association
, ...
但是,我认为路由器的构建/命名方式不是好在后者/工作案例中。因此,是否可以通过生成诸如 article_user_association
之类的命名路由,使 url_for
方法起作用(而不像 article_articles_user_association
)?
However, I think it isn't "good" the way routers are builded / named in the latter / working case. So, is it possible in some way to make the url_for
method to work by generating named routes like article_user_association
(and not like article_articles_user_association
)?
我阅读了与 ActionDispatch相关的官方文档 :: Routing :: UrlFor
方法(特别是为命名路由生成URL部分),但是我找不到解决方案。也许有一种方法可以让Rails使用特定的命名路由器,就像您想通过 self.primary_key $ c $更改表的主键列时一样。 c>语句...
I read the Official Documentation related to the ActionDispatch::Routing::UrlFor
method (in particular the "URL generation for named routes" section), but I cannot find out a solution. Maybe there is a way to "say" to Rails to use a specific named router as-like it makes when you want to change the primary key column of a table with the self.primary_key
statement...
# app/models/articles/user_association.rb
class Articles::UserAssociation < ActiveRecord::Base
# self.primary_key = 'a_column_name'
self.named_router = 'user_association'
...
end
推荐答案
您的 UserAssociation
模型位于 Articles
命名空间,该命名空间包含在命名路由中:
Your UserAssociation
model is in the Articles
namespace, which gets included in the named route:
# app/models/articles/user_association.rb
class Articles::UserAssociation < ActiveRecord::Base
...
end
# route => articles_user_association
# nested route => article_articles_user_association
如果删除名称空间,您将获得所需的路由助手:
If you remove the namespace, you will get the route helper you are looking for:
# app/models/articles/user_association.rb
class UserAssociation < ActiveRecord::Base
...
end
# route => user_association
# nested route => article_user_association
除非您有充分的理由保留 UserAssociation
不在命名空间中。
Unless you have a really good reason for keeping UserAssociation
in a namespace, don't.
这篇关于如何生成合适的嵌套资源url_for?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!