有没有人在Rails 3中管理多态嵌套资源的任何技巧? [英] Does anybody have any tips for managing polymorphic nested resources in Rails 3?

查看:61
本文介绍了有没有人在Rails 3中管理多态嵌套资源的任何技巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在config/routes.rb中:

In config/routes.rb:

resources :posts do
  resources :comments
end

resources :pictures do
  resources :comments
end

我也希望允许更多评论.

I would like to allow for more things to be commented on as well.

我当前正在使用 mongoid (mongomapper与Rails 3的兼容性不如我所愿) ,并且注释是嵌入式资源(mongoid尚不能处理多态关系资源),这意味着我确实需要父资源才能找到注释.

I'm currently using mongoid (mongomapper isn't as compatible with Rails 3 yet as I would like), and comments are an embedded resource (mongoid can't yet handle polymorphic relational resources), which means that I do need the parent resource in order to find the comment.

是否有任何优雅的方法来解决以下一些问题:

Are there any elegant ways to handle some of the following problems:

在我的控制器中,我需要先找到父级,然后才能找到评论:

In my controller, I need to find the parent before finding the comment:

if params[:post_id]
  parent = Post.find(params[:post_id]
else if params[:picture_id]
  parent = Picture.find(params[:picture_id]
end

如果我开始添加更多要评论的内容,将会变得混乱.

which is going to get messy if I start adding more things to be commentable.

url_for([comment.parent, comment])不起作用,所以我将不得不在我的Comment模型中定义一些内容,但是我想我还需要在Comment模型中定义一个索引路由以及可能的编辑和新路线定义.

Also url_for([comment.parent, comment]) doesn't work, so I'm going to have to define something in my Comment model, but I think I'm also going to need to define an index route in the Comment model as well as potentially an edit and new route definition.

随着进一步的发展,我可能还需要处理更多的问题.

There might be more issues that I have to deal with as I get further.

我无法想象我是尝试解决此问题的第一人,是否有任何解决方案可以使此问题更易于管理?

I can't imagine I'm the first person to try and solve this problem, are there any solutions out there to make this more manageable?

推荐答案

我必须在我的应用程序中做类似的事情.我接受了我想出的内容,并对其进行了一些更改,但是我尚未对其进行测试,因此请谨慎使用.它不漂亮,但是比我能想到的其他任何东西都要好.

I had to do something similar in an app of mine. I took what I came up with and changed it around a bit, but I haven't tested it, so use with care. It's not pretty, but it's better than anything else I was able to think of.

在routes.rb中:

In routes.rb:

resources :posts, :pictures

controller :comments do
  get '*path/edit' => :edit, :as => :edit_comment
  get '*path'      => :show, :as => :comment
  # etc. The order of these is important. If #show came first, it would direct /edit to #show and simply tack on '/edit' to the path param.
end

在comment.rb中:

In comment.rb:

embedded_in :commentable, :inverse_of => :comments

def to_param
  [commentable.class.to_s.downcase.pluralize, commentable.id, 'comments', id].join '/'
end

在comments_controller.rb中的before过滤器中:

In a before filter in comments_controller.rb:

parent_type, parent_id, scrap, id = params[:path].split '/'

# Security: Make sure people can't just pass in whatever models they feel like
raise "Uh-oh!" unless %w(posts pictures).include? parent_type

@parent = parent_type.singularize.capitalize.constantize.find(parent_id)
@comment = @parent.comments.find(id)

好吧,丑陋了.现在,您可以将注释添加到所需的任何模型中,只需执行以下操作即可:

Ok, ugliness over. Now you can add comments to whatever models you want, and simply do:

edit_comment_path @comment
url_for @comment
redirect_to @comment

以此类推.

我没有在自己的应用程序中实现任何其他路径,因为我只需要编辑和更新,但我想它们看起来像这样:

I didn't implement any other paths in my own app, because all I needed was edit and update, but I'd imagine they'd look something like:

controller :comments do
  get    '*path/edit' => :edit, :as => :edit_comment
  get    '*path'      => :show, :as => :comment
  put    '*path'      => :update
  delete '*path'      => :destroy
end

其他动作将更加棘手.您可能需要执行以下操作:

The other actions will be trickier. You'll probably need to do something like:

  get  ':parent_type/:parent_id/comments'     => :index, :as => :comments
  post ':parent_type/:parent_id/comments'     => :create
  get  ':parent_type/:parent_id/comments/new' => :new,   :as => :new_comment

然后您将使用params [:parent_type]和params [:parent_id]在控制器中访问父模型.您还需要将适当的参数传递给url帮助器:

You'd then access the parent model in the controller using params[:parent_type] and params[:parent_id]. You'd also need to pass the proper parameters to the url helpers:

comments_path('pictures', 7)

这篇关于有没有人在Rails 3中管理多态嵌套资源的任何技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆