为什么我的一些Rails路径助手有一个_index后缀? [重复]

时间:2022-09-25 14:12:25

This question already has an answer here:

这个问题在这里已有答案:

I have a Rails route definition that looks something like this:

我有一个Rails路由定义,看起来像这样:

namespace :admin do
  resources :feeds
  resources :push
end

rake routes generates the following output for it:

rake routes为它生成以下输出:

admin_feeds GET  /admin/feeds  {:controller=>"admin/feeds", :action=>"index"}
admin_push_index GET  /admin/push  {:controller=>"admin/push", :action=>"index"}

Why would would the path helper for push get the _index suffix, but not feeds?

为什么push的路径助手会获得_index后缀,而不是feed?

2 个解决方案

#1


15  

It's all based on the plurality of the resource. So if the resource name is plural, then it has no need to add an _index suffix since it's inferred.

这一切都基于多个资源。因此,如果资源名称是复数,那么它不需要添加_index后缀,因为它是推断的。

If it is a singular resource name, then it adds the suffix for clarification since admin_push would typically be a show action instead of the index action.

如果它是一个单一的资源名称,那么它会添加后缀以便澄清,因为admin_push通常是show动作而不是index动作。

#2


4  

You can also use

你也可以使用

resource :push

instead of

代替

resources :push

to specify a singular resource. See http://api.rubyonrails.org/classes/ActionDispatch/Routing.html

指定一个单一的资源。请参阅http://api.rubyonrails.org/classes/ActionDispatch/Routing.html

#1


15  

It's all based on the plurality of the resource. So if the resource name is plural, then it has no need to add an _index suffix since it's inferred.

这一切都基于多个资源。因此,如果资源名称是复数,那么它不需要添加_index后缀,因为它是推断的。

If it is a singular resource name, then it adds the suffix for clarification since admin_push would typically be a show action instead of the index action.

如果它是一个单一的资源名称,那么它会添加后缀以便澄清,因为admin_push通常是show动作而不是index动作。

#2


4  

You can also use

你也可以使用

resource :push

instead of

代替

resources :push

to specify a singular resource. See http://api.rubyonrails.org/classes/ActionDispatch/Routing.html

指定一个单一的资源。请参阅http://api.rubyonrails.org/classes/ActionDispatch/Routing.html