0

I'm hoping this is a really simple thing that I'm just not seeing. I'm fairly new to rails so I imagine this is right under my nose.

My app has what you call stacks and I created a resource for it:

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

I want to change the route for the show action from /stacks/:id to /stack/:id

I had added this above the resource, but it wasn't working:

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

Thanks!

Edit: code

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

1 回答 1

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

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

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

这应该为您解决问题。

于 2013-11-15T08:19:30.673 回答