连接模型的Rails 3资源路由 [英] Rails 3 Resource routing for join models

查看:59
本文介绍了连接模型的Rails 3资源路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与用户和团队之间存在多对多关系(如has_many:through),我正在尝试建立团队成员加入模型作为路线中的资源。



在我的布局中,我设置了一个团队上下文表单,该表单为 current_team 设置了一个会话变量,我希望该路线将team_members资源定义为 / team_members /:user_id / show

我是否尝试过使用 resources:team_members 在route.rb中做到这一点? :path_names => {:action => \some\url} ,但是对于需要:id的操作,路由器会将路由附加为 \:id\some\ url

解决方案

编辑:
如果您希望能够编辑团队成员身份,您可以拥有

 资源:用户需要
资源:team_members
结束

然后编辑成员资格=> / users /:user_id / team_members /:id /编辑



然后您可以在team_members_controller中执行任何操作。



或者如数字1311407所述,只需 resources:team_members ,您将拥有与团队成员一起使用的所有其他途径。






真的不想要标准的 / teams /:team_id / users /:id 吗?



如果您真的想要 / team_members /:user_id / show
您可以做

  get / team_members /:id / show =>  users#show 

但我认为这不是个好主意。


I have a many-to-many relationship between users and teams (as a has_many :through), and I'm trying to setup a "team members" join model as a resource in the routes.

In my layouts I've setup a "team context form" that sets a session variable for the current_team, and I want the route for the team_members resources to be defined as /team_members/:user_id/show. Is there any way to do this with the resources :team_members in routes.rb?

I've tried using :path_names => {:action => "\some\url"}, however for actions that require an :id the router appends the route to be something like "\:id\some\url"

解决方案

edit: If you want to be able to edit the team membership, you could have

resources :users do
    resources :team_members
end

and then, to edit the membership => /users/:user_id/team_members/:id/edit

And then you can do whatever you want in the team_members_controller.

Or as numbers1311407 said, just resources :team_members and you'll have all the rest routes to work with the team memberships.


Really don't want the standard /teams/:team_id/users/:id ?

If you really want /team_members/:user_id/show You could just do

get "/team_members/:id/show" => "users#show"

But I dont think it's a good idea.

这篇关于连接模型的Rails 3资源路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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