If I want a custom slug
I walk through this cutstom slug.
- create migration
add_slug
to add a column inside thePost
generate_slug
usingbefore_save
. Read all the ActiveRecord Callbacks- If you want to find a specific
Post
, you can not usePost.find
, you have to usePost.find_by slug: 'my-post'
RAILS g migration add_slug
# in migration
def change
add_column :users, :slug, :string
end
#/model/post
after_validation
#OR before_save(every single time after savei it ),
#OR before_create(only call once)
def generate_slug
self.slug = self.title.gsub(' ', '-').downcase
# self.slug = SecureRandom.base64(6)
end
#in rails c
Post.all.each { |post| post.save }
#overwrite the to_param method
def to_param
self.slug
end
#in controllers
# post = Post.find(params[:id])
#should be
post = Post.find_by slug: params[:id]
If you look up the source code of Rails:
@model ActiveREcord::Integration
# File activerecord/lib/active_record/integration.rb, line 40
def to_param
# We can't use alias_method here, because method 'id' optimizes itself on the fly.
id && id.to_s # Be sure to stringify the id for routes
end
user = User.find_by(name: 'Phusion')
user_path(user) # => "/users/1"
# overide to_param
class User < ActiveRecord::Base
def to_param # overridden
name
end
end
user = User.find_by(name: 'Phusion')
user_path(user) # => "/users/Phusion"
There are some resources I recommend to understand how the to_param
work: