更改特定操作的路由资源名称

时间:2013-11-15 07:45:30

标签: ruby-on-rails ruby-on-rails-4 rails-routing

我希望这是一件非常简单的事情,我只是没有看到。我对rails很新,所以我想这就在我的鼻子底下。

我的应用程序有你所谓的stacks,我为它创建了一个资源:

resources :stacks, only: [:new, :create, :show]

我想将展示操作的路线从/stacks/:id更改为/stack/:id

我已将此添加到资源上方,但它无效:

get '/stacks/:id', to: 'stacks#show', as: 'stack'

谢谢!

编辑:代码

get '/stacks/:id', to: 'stacks#show', as: 'stack'
resources :stacks, only: [:new, :create, :show]

1 个答案:

答案 0 :(得分:1)

出于路由,数据库表名等原因,Ruby on Rails默认复制资源名称,但您可以覆盖此行为的特定情况。

转到Ruby on Rails应用程序文件夹(我们只是将它称为'.app',用于我们的目的)。在编辑器中打开.app / config / initializers / inflections.rb,您将看到以下内容(Rails 4):

# Be sure to restart your server when you modify this file.

# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
#   inflect.plural /^(ox)$/i, '\1en'
#   inflect.singular /^(ox)en/i, '\1'
#   inflect.irregular 'person', 'people'
#   inflect.uncountable %w( fish sheep )
# end

# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
#   inflect.acronym 'RESTful'
# end

添加一个Inflector,使“stack”是“stack”的复数形式,如下所示:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'stack', 'stack'
end

这应该适合你。