Rails RESTful 路由:覆盖 params[:id] 或 params[:model_id] 默认值

发布于 2024-08-12 05:06:12 字数 1098 浏览 13 评论 0原文

我试图了解如何直接在 map.resources 上更改此规则:

假设我有一条路线:

map.resource :user, :as => ':user', :shallow => true do |user|
    user.resources :docs, :shallow => true do |file|
        file.resources :specs
    end
end

所以我会有这样的 RESTful 路线:

/:user/docs

/docs/:id

/docs/:doc_id/规格

所以我发现在这种情况下很难跟踪 params[:doc_id] 因为有时它的 params[:id] 有时它的 params [:doc_id] 在这种情况下,我希望始终调用一个特定名称,这样我就不必为我的过滤器创建两个不同的声明。

好吧,我做了一些研究,发现了这个补丁:

http://dev.rubyonrails.org /ticket/6814

基本上,它的作用是让您能够在 map.resources 上添加 :key 参数,这样您就可以定义稍后如何引用它,这样我们就可以得到类似的东西:

map.resources :docs, :key => :doc ...

所以我总是会用 params[:doc] 来调用参数。

但实际上这个补丁有点旧(现在已经3年了) 所以我想知道我们是否没有任何更新的和内置的 Rails 来完成这项任务?

PS 我不确定模型中定义的 to_param 方法,显然这并没有改变我的请求以及我仍然收到的日志: 参数:{"doc_id"=>"6"}参数:{"id"=>"6"} 始终。

I'm trying to understand how to change this rule directly on the map.resources:

supposing I have a route:

map.resource :user, :as => ':user', :shallow => true do |user|
    user.resources :docs, :shallow => true do |file|
        file.resources :specs
    end
end

so I would have RESTful routes like this:

/:user/docs

/docs/:id

/docs/:doc_id/specs

So I see that is difficult to track the params[:doc_id] on this case because sometimes its params[:id] and sometimes its params[:doc_id] and in this case I would like to always call for one specific name so I won't have to create two different declarations for my filters.

Well, I did a little bit of research and I found this patch:

http://dev.rubyonrails.org/ticket/6814

and basically what this does is give you the ability to add a :key parameter on you map.resources so you can defined how you would like to reference it later so we could have something like:

map.resources :docs, :key => :doc ...

so I always would call the param with params[:doc] instead.

But actually this patch is a little bit old (3 years now)
so I was wondering if we don't have anything newer and already built-in for rails to do this task?

P.S I'm not sure about that to_param method defined inside the model, apparently this didn't change anything on my requests, and on the logs I still getting:
Parameters: {"doc_id"=>"6"} or Parameters: {"id"=>"6"} all the time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

葬心 2024-08-19 05:06:12

在不编写完全自定义路由的情况下使参数更加友好的一种方法是,

# docs_controller.rb
def show
  @doc = Doc.find(params[:doc].to_i)
end

# doc.rb
def to_param
  [ id, permalink ].join("-")
  # assumes you're storing a permalink formatted version of the name of your doc
end

# routes.rb
map.resources :docs

这将为您提供类似于 example.com/docs/234-the-name-of-your-doc 的 URL

One method of making the parameters a little more friendly without writing fully custom routes is

# docs_controller.rb
def show
  @doc = Doc.find(params[:doc].to_i)
end

# doc.rb
def to_param
  [ id, permalink ].join("-")
  # assumes you're storing a permalink formatted version of the name of your doc
end

# routes.rb
map.resources :docs

This will give you URLs that look something like example.com/docs/234-the-name-of-your-doc

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