खोज…


परिचय

फ्रेंडलीआईड एक्टिव रिकॉर्ड के लिए स्लगिंग और पर्मलिंक प्लग का "स्विस आर्मी बुलडोजर" है। यह आपको सुंदर URL बनाने और मानव-अनुकूल स्ट्रिंग्स के साथ काम करने देता है जैसे कि वे संख्यात्मक आईडी थे। FriendlyId के साथ, आपके एप्लिकेशन को URL जैसे उपयोग करना आसान है:

http://example.com/states/washington

रेलस्टार्ट रेल

rails new my_app
cd my_app

Gemfile

gem 'friendly_id', '~> 5.1.0' # Note: You MUST use 5.0.0 or greater for Rails 4.0+
rails generate friendly_id
rails generate scaffold user name:string slug:string:uniq
rake db:migrate

ऐप / मॉडल / user.rb संपादित करें

class User < ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: :slugged
end

User.create! name: "Joe Schmoe"

# Change User.find to User.friendly.find in your controller
User.friendly.find(params[:id])

rails server
GET http://localhost:3000/users/joe-schmoe

# If you're adding FriendlyId to an existing app and need
# to generate slugs for existing users, do this from the
# console, runner, or add a Rake task:
User.find_each(&:save)

Finders are no longer overridden by default. If you want to do friendly finds, you must do Model.friendly.find rather than Model.find. You can however restore FriendlyId 4-style finders by using the :finders addon


friendly_id :foo, use: :slugged # you must do MyClass.friendly.find('bar')
#or...
friendly_id :foo, use: [:slugged, :finders] # you can now do MyClass.find('bar')

एक नया "उम्मीदवार" कार्यक्षमता जो वैकल्पिक स्लग की एक सूची को सेट करना आसान बनाता है, जिसका उपयोग अनुक्रम को जोड़ने के बजाय विशिष्ट रूप से रिकॉर्ड को भेद करने के लिए किया जा सकता है। उदाहरण के लिए:

class Restaurant < ActiveRecord::Base
  extend FriendlyId
  friendly_id :slug_candidates, use: :slugged

  # Try building a slug based on the following fields in
  # increasing order of specificity.
  def slug_candidates
    [
      :name,
      [:name, :city],
      [:name, :street, :city],
      [:name, :street_number, :street, :city]
    ]
  end
end

दोस्ताना_id रत्न का उपयोग करके स्लग सीमा लंबाई सेट करें?

def normalize_friendly_id(string)
   super[0..40]
end


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow