Rails で検索フレンドリーな URL を作成する方法

Display IDs in your URLs can be terrible and wasteful for SEO. In this blog entry, I will demonstrate to you how you can utilize slugs to make your URLs more relevant to the page content. In the first place, we will utilize an implicit arrangement (superseding the to_param strategy) to change the URLs and a while later, we will utilize a gem called friendly_id to produce slugs and keep up a past filled with these slugs. So that search engines index your web pages. For example: http://www.exaple.com/blogs/1http://www.exaple.com/blogs/blog-name ここでは 1 つのサンプルを作成しています レール5 ポストモデルを備えたアプリを使用して、きれいな URL でどのように正確に機能するかを示します。
Rails G scaffold 投稿タイトル:文字列 公開:ブール値
走る Rails データベース:移行 いくつかのサンプル投稿を作成します(http://localhost:3000/posts/new) app/models/post.rb
def to_param title.parameterize end
All the links will be changed to pretty URLs once you add the above method to your model. Please check on index page where it lists all the posts. However, when you click on that it will show you error page. Post.find method cannot find the record with id which we are passing in URL. We need to create slugs for the above posts in our db for that model. Let’s add migration file for the slug
Rails g 移行 AddSlugToPosts slug:string
上記の移行ファイルを開き、その sulg に uniq インデックスを追加します。スラッグは一意である必要があります。
class AddSlugToPosts < ActiveRecord::Migration[5.0] def change add_column :posts, :slug, :string add_index :posts, :slug, unique: true end end
走る データベース:移行 Now, we have to change our show action db query to find_by instead of find method. Because we need to fetch the results based on slug. Change our set_post method to
def set_post Post.find_by_slug(params[:id]) 終了
これで、きれいな URL で期待どおりに動作するようになりました。 2 つのレコードの名前が同じであるため、2 つのレコードの URL が同じ場合はどうなるでしょうか。 .find_by は常に最初のレコードをフェッチするためです。これを解決するには、スラッグを保存する前に、以下で説明する方法でレコード ID をスラッグに結合できます。
def add_slug “#{id}-#{title.parameterize}” end
So, it will generate like “1-blog-name”. We have a gem called friendly_gem to simply take care of all the above-mentioned scenarios. Feel free to remove the above mentione code from the sample app because everything is managed by the gem. Friendly_id gem(Rails 5) Add the latest gem from rubygem web to the gemfile and run bundle
gem 'friend_id', '~> 5.1'
レールは Friendly_id を生成します
上記のコマンドは、初期化ファイルと移行ファイルを生成します。
db:移行を実行します
いくつかのレコードを作成し、set_post メソッドを以下に示す方法に変更します。古いレコードがある場合は、すべて再生成する必要があります。
def set_post Post.friend.find("Joe Schmoe".parameterize) end
app/models/post.rb
FriendlyId Friendly_id :slug_candidates を拡張、使用: [:slugged, :finders, :history] def slug_candidates [:title, [:title, :id] ] end
Post モデルに slug 属性を追加する
Rails g AddSlugToPosts slug:string
add_column :投稿、:スラッグ、:文字列
add_index :posts、:slug、一意: true
投稿タイトルを更新しています title 属性を更新すると、Slug も自動的に更新されます。ただし、スラッグを頻繁に更新しすぎるのは得策ではありません。古いブックマークを持っている可能性があるため、ブックマークされた URL をクリックするとアプリケーションでエラーがスローされる場合があります。これを防ぐには、Post モデルの use オプションに履歴シンボルを追加する必要があります。
def slug_candidates [:title, [:title, :id] ] 終了
slug_candidates feature was added in friendlyId 5. It allows us to tell the friendly id gem, what needs to be done in case of duplicate slugs. The code mentioned above advises FriendlyId that it needs to utilize the slug_candidates technique to make the slug uniq. It will attempt the rundown right from the beginning to the end, so in the case above, it will attempt to produce the slug first utilizing the title, and afterwards, if a post by that title already exists, it will attempt to fix the ID again. Slugs are the primary things read by search engines. That ought to, as of now, give you an idea on how essential they are. To utilize a slug, first add the slug alternative to the friendly_id assistant in the Post Model.
関連記事

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

jaJapanese