So erstellen Sie suchfreundliche URLs in Rails

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/1 Zu http://www.exaple.com/blogs/blog-name Hier erstelle ich ein Beispiel Schienen5 App mit Post-Modell, um zu veranschaulichen, wie genau es mit den hübschen URLs funktionieren wird.
Schienen g Gerüstbeitrag Titel:String veröffentlicht:boolean
laufen Schienen db: migrieren und einige Beispielbeiträge erstellen (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
Schienen g Migration AddSlugToPosts slug:string
Öffnen Sie die oben genannte Migrationsdatei und fügen Sie diesem Sulg den Uniq-Index hinzu. Slug muss eindeutig sein.
Klasse AddSlugToPosts < ActiveRecord::Migration[5.0] def change add_column :posts, :slug, :string add_index :posts, :slug, unique: true end end
laufen db:migrieren 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]) end
Jetzt funktioniert es wie erwartet mit den hübschen URLs. Was passiert, wenn zwei Datensätze dieselbe URL haben, da beide Datensätze denselben Namen haben? Weil .find_by immer den ersten Datensatz abruft. Um dieses Problem zu lösen, können wir vor dem Speichern des Slugs die Datensatz-ID wie unten beschrieben mit dem Slug kombinieren:
def add_slug „#{id}-#{title.parameterize}“ Ende
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-Juwel(Rails 5) Add the latest gem from rubygem web to the gemfile and run bundle
gem 'Friendly_id', '~> 5.1'
Schienen generieren eine freundliche_ID
Der obige Befehl generiert eine Initialisierungsdatei und eine Migrationsdatei.
Führen Sie db:migrate aus
Erstellen Sie einige Datensätze und ändern Sie unsere set_post-Methode wie unten beschrieben. Alle alten Datensätze müssen neu generiert werden, falls Sie welche haben.
def set_post Post.Friendly.find("Joe Schmoe".parameterize) end
app/models/post.rb
erweitern FriendlyId Friendly_id :slug_candidates, use: [:slugged, :finders, :history] def slug_candidates [ :title, [:title, :id] ] end
Slug-Attribut zum Post-Modell hinzufügen
Schienen g AddSlugToPosts slug:string
add_column :posts, :slug, :string
add_index :posts, :slug, unique: true
Beitragstitel wird aktualisiert Slug wird automatisch aktualisiert, wenn Sie das Titelattribut aktualisieren. Es ist jedoch keine gute Vorgehensweise, die Slugs zu häufig zu aktualisieren. Die Anwendung kann beim Klicken auf eine der mit Lesezeichen versehenen URLs Fehler auslösen, da möglicherweise alte Lesezeichen vorhanden sind. Wir müssen dies verhindern, indem wir der Verwendungsoption in Ihrem Post-Modell ein Verlaufssymbol hinzufügen.
def slug_candidates [ :title, [:title, :id] ] end
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.
zusammenhängende Posts

Hinterlasse einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

de_DEGerman