将参数名称更改为嵌套资源父级 [英] Change param name to nested resource parent

查看:39
本文介绍了将参数名称更改为嵌套资源父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的路线:

resources :tags do
      resources :comments
end

所以评论的 :create 操作具有以下形式

so the :create action for the comments has the following form

tag_comments POST   /tags/:tag_id/comments(.:format)

如何将参数名称从 :tag_id 更改为 :commentable_id?

how can i change the paramenter name from :tag_id to :commentable_id?

推荐答案

map.tags do
  resources :comments, :path_prefix => '/tags/:commentable_id'
end

或通过 before_filter

or via before_filter

before_filter :tag2commentable

private
def tag2commentable
  params[:commentable_id] = params[:tag_id] unless params[:tag_id].blank?
end

把它放在你的控制器中

这篇关于将参数名称更改为嵌套资源父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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