···11+# FriendlyId Global Configuration
22+#
33+# Use this to set up shared configuration options for your entire application.
44+# Any of the configuration options shown here can also be applied to single
55+# models by passing arguments to the `friendly_id` class method or defining
66+# methods in your model.
77+#
88+# To learn more, check out the guide:
99+#
1010+# http://norman.github.io/friendly_id/file.Guide.html
1111+1212+FriendlyId.defaults do |config|
1313+ # ## Reserved Words
1414+ #
1515+ # Some words could conflict with Rails's routes when used as slugs, or are
1616+ # undesirable to allow as slugs. Edit this list as needed for your app.
1717+ config.use :reserved
1818+1919+ config.reserved_words = %w[new edit index session login logout users admin
2020+ stylesheets assets javascripts images]
2121+2222+ # This adds an option to treat reserved words as conflicts rather than exceptions.
2323+ # When there is no good candidate, a UUID will be appended, matching the existing
2424+ # conflict behavior.
2525+2626+ # config.treat_reserved_as_conflict = true
2727+2828+ # ## Friendly Finders
2929+ #
3030+ # Uncomment this to use friendly finders in all models. By default, if
3131+ # you wish to find a record by its friendly id, you must do:
3232+ #
3333+ # MyModel.friendly.find('foo')
3434+ #
3535+ # If you uncomment this, you can do:
3636+ #
3737+ # MyModel.find('foo')
3838+ #
3939+ # This is significantly more convenient but may not be appropriate for
4040+ # all applications, so you must explicitly opt-in to this behavior. You can
4141+ # always also configure it on a per-model basis if you prefer.
4242+ #
4343+ # Something else to consider is that using the :finders addon boosts
4444+ # performance because it will avoid Rails-internal code that makes runtime
4545+ # calls to `Module.extend`.
4646+ #
4747+ # config.use :finders
4848+ #
4949+ # ## Slugs
5050+ #
5151+ # Most applications will use the :slugged module everywhere. If you wish
5252+ # to do so, uncomment the following line.
5353+ #
5454+ # config.use :slugged
5555+ #
5656+ # By default, FriendlyId's :slugged addon expects the slug column to be named
5757+ # 'slug', but you can change it if you wish.
5858+ #
5959+ # config.slug_column = 'slug'
6060+ #
6161+ # By default, slug has no size limit, but you can change it if you wish.
6262+ #
6363+ # config.slug_limit = 255
6464+ #
6565+ # When FriendlyId can not generate a unique ID from your base method, it appends
6666+ # a UUID, separated by a single dash. You can configure the character used as the
6767+ # separator. If you're upgrading from FriendlyId 4, you may wish to replace this
6868+ # with two dashes.
6969+ #
7070+ # config.sequence_separator = '-'
7171+ #
7272+ # Note that you must use the :slugged addon **prior** to the line which
7373+ # configures the sequence separator, or else FriendlyId will raise an undefined
7474+ # method error.
7575+ #
7676+ # ## Tips and Tricks
7777+ #
7878+ # ### Controlling when slugs are generated
7979+ #
8080+ # As of FriendlyId 5.0, new slugs are generated only when the slug field is
8181+ # nil, but if you're using a column as your base method can change this
8282+ # behavior by overriding the `should_generate_new_friendly_id?` method that
8383+ # FriendlyId adds to your model. The change below makes FriendlyId 5.0 behave
8484+ # more like 4.0.
8585+ # Note: Use(include) Slugged module in the config if using the anonymous module.
8686+ # If you have `friendly_id :name, use: slugged` in the model, Slugged module
8787+ # is included after the anonymous module defined in the initializer, so it
8888+ # overrides the `should_generate_new_friendly_id?` method from the anonymous module.
8989+ #
9090+ # config.use :slugged
9191+ # config.use Module.new {
9292+ # def should_generate_new_friendly_id?
9393+ # slug.blank? || <your_column_name_here>_changed?
9494+ # end
9595+ # }
9696+ #
9797+ # FriendlyId uses Rails's `parameterize` method to generate slugs, but for
9898+ # languages that don't use the Roman alphabet, that's not usually sufficient.
9999+ # Here we use the Babosa library to transliterate Russian Cyrillic slugs to
100100+ # ASCII. If you use this, don't forget to add "babosa" to your Gemfile.
101101+ #
102102+ # config.use Module.new {
103103+ # def normalize_friendly_id(text)
104104+ # text.to_slug.normalize! :transliterations => [:russian, :latin]
105105+ # end
106106+ # }
107107+end
+6-9
config/routes.rb
···11Rails.application.routes.draw do
22- get 'tags/index'
33- get 'tags/create'
44- resources :galleries
52 # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
6374 # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
···1310end
14111512Rails.application.routes.draw do
1616- get 'tags/index'
1717- get 'tags/create'
1813 get "/tags", to: "tags#index"
1414+# get "tags/:edit" => "tags#edit", as: :edit_tag_path
1915 get "tags/:id" => "tags#show", as: :tag
1616+ get "tags/:new" => "tags#new"
1717+# resources :tag, path: '', only: [:show]
1818+ resources :tags
1919+end
20202121- resources :galleries
2121+Rails.application.routes.draw do
2222 get "/articles", to: "articles#index"
2323-# get 'tags/:tag', to: 'articles#index', as: "tag"
2423 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
2524end
26252726Rails.application.routes.draw do
2828- resources :galleries
2927 get "/users", to: "users#index", via: "get"
3028end
31293230Rails.application.routes.draw do
3333- resources :galleries
3431 root "articles#index"
35323633 resources :profiles
+38-3
test/controllers/tags_controller_test.rb
···11require "test_helper"
2233class TagsControllerTest < ActionDispatch::IntegrationTest
44+ setup do
55+ @tag = tags(:one)
66+ end
77+48 test "should get index" do
55- get tags_index_url
99+ get tags_url
1010+ assert_response :success
1111+ end
1212+1313+ test "should get new" do
1414+ get new_tag_url
1515+ assert_response :success
1616+ end
1717+1818+ test "should create tag" do
1919+ assert_difference("Tag.count") do
2020+ post tags_url, params: { tag: { name: @tag.name, slug: @tag.slug } }
2121+ end
2222+2323+ assert_redirected_to tag_url(Tag.last)
2424+ end
2525+2626+ test "should show tag" do
2727+ get tag_url(@tag)
628 assert_response :success
729 end
83099- test "should get create" do
1010- get tags_create_url
3131+ test "should get edit" do
3232+ get edit_tag_url(@tag)
1133 assert_response :success
3434+ end
3535+3636+ test "should update tag" do
3737+ patch tag_url(@tag), params: { tag: { name: @tag.name, slug: @tag.slug } }
3838+ assert_redirected_to tag_url(@tag)
3939+ end
4040+4141+ test "should destroy tag" do
4242+ assert_difference("Tag.count", -1) do
4343+ delete tag_url(@tag)
4444+ end
4545+4646+ assert_redirected_to tags_url
1247 end
1348end