使用可选的belongs_to/nested继承资源渲染多态部分(Rails 3.1)

发布于 2024-12-01 02:11:34 字数 1652 浏览 2 评论 0原文

尝试做一些非常简单的事情——有人可以提供正确的咒语吗?

基本上,我们有 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)
        &nbsp;|&nbsp;
      %td
        = link_to 'Edit', edit_resource_url(biscuit)

当从 /users/1/biscuits 处的 BiscuitsController 调用时,该部分工作正常,但它会在 Users#edit 中出现 NoMethodError 错误从位于 /users/1/editUsersController 调用 # 的未定义方法“user_url” - 似乎是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界等同你 2024-12-08 02:11:34

关于路由行为 - 据我所知,如果你想使用嵌套和非嵌套的路由,你真的应该定义它两次。也就是说

resources :users do
  resources :biscuits
end
resources :biscuits

,只有在没有嵌套的情况下访问资源没有意义时,才嵌套资源似乎被认为是更好的做法。如果您只保留饼干的非嵌套路线,它可能会解决您的问题。

关于resource_url - 您对resource_url和集合方法的引用表明您正在使用插件或gem,如果不知道您正在使用什么插件,就很难知道出了什么问题,特别是因为它似乎是您的资源的来源问题。

将“resource_url”替换为如下调用的帮助程序

def nested_resource_path(*args)
  args = args.compact
  return args[0] if args.size == 1
  return args
end

nested_resource_path(@user, biscuit) and
nested_resource_path(:edit, @user, biscuit)

访问 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.

resources :users do
  resources :biscuits
end
resources :biscuits

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

def nested_resource_path(*args)
  args = args.compact
  return args[0] if args.size == 1
  return args
end

called thus:

nested_resource_path(@user, biscuit) and
nested_resource_path(:edit, @user, biscuit)

where @user is nil when accessing BiscuitsController#index
should work.

千里故人稀 2024-12-08 02:11:34

卡入浅路线,即:

resources :users, :shallow => true do
  resources :biscuits
end

然后进行耙路线,看看是否有效

chuck in a shallow route, that is:

resources :users, :shallow => true do
  resources :biscuits
end

then do rake routes and see if that does the trick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文