Ruby post title to slug
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
downcase
makes it lowercase. The strip
makes sure there is no leading or trailing whitespace. The first gsub
replaces spaces with hyphens. The second gsub
removes all non-alpha non-dash non-underscore characters (note that this set is very close to \W
but includes the dash as well, which is why it's spelled out here).
How to change slug before use in friendly_id gem?
You have a combination of two problems:
- You are literally passing the symbol
:title
to your.convert_slug
method, so 100% of the time it just converts that to the string 'title' and returns it. - The first parameter to the
friendly_id
thing is not the slug, it is the name (string or symbol) of a method that will be called to obtain the slug.
So, because of #1 the first parameter is always the string 'title', which friendly_id
then dutifully calls as a method and the result is your original title value.
Wrapping your code in another method will get you what you want:
class CompanyJob < ApplicationRecord
extend FriendlyId
def safe_slug
slug = title.downcase.to_s
# ... your .gsubs
slug
end
friendly_id :safe_slug, use: :slugged
Rails slugs in URL - using Active Record Model Post's Title attribute instead of ID
find_by_foo is not something that you need to define. ActiveRecord will take of it for you, as long as you have a column named "foo". Adding an exclamation point like you did will cause an exception to be thrown if no record is found, as opposed to returning nil without the exception if you don't use the exclamation point.
As for your specific issue, you added your slug to Post, but you're trying to search on Project.
Implementing /YYYY/MM/Title-Slug URL structure with Friendly_Id
I've got a partially-working solution that can be viewed in a new question I've just created. The index and individual posts render as desired, but I'm running into issues when creating & editing posts and I don't have a solution for yearly indexes e.g., http://www.example.com/2015/
and monthly indexes e.g., http://www.example.com/2015/09/
.
Additional insight on resolving these outstanding issues at the new question would be really appreciated!
A full, working implementation can be found in the accepted answer of this subsequent question.
How to set the id of a post to it's title
I am suggesting to use "Friendly_id" in LINK, it allows to use .find
with slug.
Usage:
# both will be available
/links/5
/links/product-name
Installation:
# in gemfile
gem 'friendly_id', '~> 5.1.0'
Install in rails
bundle install
rails generate friendly_id
rails g migration add_slug_to_links slug:string:uniq
rake db:migrate
Link.rb
class Link < ActiveRecord::Base
extend FriendlyId
friendly_id :product, use: :slugged
Controller
def create
@link = Link.new(link_params)
@link.save
end
Related Topics
How to Authorize a Service Account for Google Calendar API in Ruby
Generate Unique Random String with Letters and Numbers in Lower Case
Accessing Config from Application.Rb in Controller (Rails 3)
What's the Opposite of Chr() in Ruby
Attr_Accessor on Class Variables
Installing the Postgresql Gem with 'Gem Pq' Fails with Error: Failed to Build Gem Native Extension
Ruby Way to Generate a Hmac-Sha1 Signature for Oauth
Disable a Group of Tests in Rspec
Rails Mapping Array of Hashes Onto Single Hash
Finding What Is Common to Two Arrays
How to Get the Number of Days in a Given Month in Ruby, Accounting for Year
If Java People Go to Scala, C# Go to F#, Where Do Ruby People Go for Functional Nirvana
Generating Unique, Hard-To-Guess "Coupon" Codes
How to Remove Carriage Returns with Ruby
When Do We Use the "||=" Operator in Rails? What Is Its Significance
Check Whether a String Contains One of Multiple Substrings