使用可选的belongs_to/nested继承资源渲染多态部分(Rails 3.1)
尝试做一些非常简单的事情——有人可以提供正确的咒语吗?
基本上,我们有 Biscuit
可选地嵌套在 User
中,所以我们想要这样的路由:
/biscuits
/biscuits/1
/users/2/biscuits
/users/2/biscuits/3
等等。
我们有像 biscuits/index
这样的视图,它调用部分biscuits/_index
呈现列表。我想从用户的个人资料视图中调用相同的部分,users/edit
,但我不清楚要使用哪个resource_url帮助器:
resources :users do
resources :biscuits
end
class BiscuitsController < InheritedResources::Base
belongs_to :user, optional: true
end
users/edit.html.haml:
= render 'biscuits/index', biscuits: @user.biscuits.all
biscuits/_index。 html.haml:
- biscuits.each do |biscuit|
%tr
%td= biscuit.title
%td= link_to image_tag(biscuit.file_url(:thumb,:large)), resource_url(biscuit)
%td
= link_to 'Show', resource_url(biscuit)
|
%td
= link_to 'Edit', edit_resource_url(biscuit)
当从 /users/1/biscuits
处的 BiscuitsController
调用时,该部分工作正常,但它会在 Users#edit 中出现 NoMethodError 错误从位于
的未定义方法“user_url” - 似乎是 /users/1/edit
的 UsersController
调用 #resource_url< /code> 这里指的是用户而不是饼干集合。
无论当前控制器如何,如何强制资源/集合成为任何资源集合?
有什么更好的方法来做到这一点?
另外,假设我们覆盖 UsersController#collection
和 #resource
,如果路由调用 BiscuitsController<,则
UsersController
上的这些方法是否会被调用/code> 通过 /user/1/biscuits
?或者每个请求只有一个 Controller
由 Rails 实例化?
Trying to do something really simple--could someone please provide the correct incantation?
Basically we have Biscuit
optionally nested in User
so we'd like routes like:
/biscuits
/biscuits/1
/users/2/biscuits
/users/2/biscuits/3
etc.
We have views like biscuits/index
which calls a partial biscuits/_index
to render the list. I'd like to call this same partial from the user's profile view, users/edit
, but I'm unclear on which resource_url helpers to use:
resources :users do
resources :biscuits
end
class BiscuitsController < InheritedResources::Base
belongs_to :user, optional: true
end
users/edit.html.haml:
= render 'biscuits/index', biscuits: @user.biscuits.all
biscuits/_index.html.haml:
- biscuits.each do |biscuit|
%tr
%td= biscuit.title
%td= link_to image_tag(biscuit.file_url(:thumb,:large)), resource_url(biscuit)
%td
= link_to 'Show', resource_url(biscuit)
|
%td
= link_to 'Edit', edit_resource_url(biscuit)
This partial works fine when called from the BiscuitsController
at /users/1/biscuits
, but it bombs with NoMethodError in Users#edit undefined method 'user_url' for #<UsersController>
when called from the UsersController
at /users/1/edit
-- seems the resource_url
refers to the user here not the biscuits collection.
How could I force the resource/collection to be any collection of resources, regardless of the current controller?
What's the better way to do this?
Also, say we override UsersController#collection
and #resource
, are these methods on the UsersController
called if the route invokes the BiscuitsController
via /users/1/biscuits
? Or is only one Controller
per request ever instantiated by Rails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于路由行为 - 据我所知,如果你想使用嵌套和非嵌套的路由,你真的应该定义它两次。也就是说
,只有在没有嵌套的情况下访问资源没有意义时,才嵌套资源似乎被认为是更好的做法。如果您只保留饼干的非嵌套路线,它可能会解决您的问题。
关于resource_url - 您对resource_url和集合方法的引用表明您正在使用插件或gem,如果不知道您正在使用什么插件,就很难知道出了什么问题,特别是因为它似乎是您的资源的来源问题。
将“resource_url”替换为如下调用的帮助程序
:
访问 BiscuitsController#index 时,其中 @user 为 nil
应该有效。
regarding routes behavior - as far as I know, if you want to use a route both nested and non-nested, you really should define it twice. i.e.
that said, it seems to be considered better practice to nest resources only if there is no sense to access them without nesting. if you leave only the non-nested routes for biscuits it might solve your problem.
regarding resource_url - your references to resource_url and collection methods indicate you are using a plugin or gem, it's hard to know what's going wrong without knowing what plugin you are using especially since it seems to be the source of your problem.
substituting "resource_url" for a helper along the lines of
called thus:
where @user is nil when accessing BiscuitsController#index
should work.
卡入浅路线,即:
然后进行耙路线,看看是否有效
chuck in a shallow route, that is:
then do rake routes and see if that does the trick