Update
A solution was found while writing this question. Still posting the question in case someone else has the same issue. My solution was because of a typo. See bottom of this post and the posted answer for complete code.
Original Question
Trying to implement the friendlyId gem in a RoR webapp.
I have already asked this question on the GitHub forum , but saw in the Wiki notes to post my question here. I did do a search for an answer already and tried multiple suggestions, and nothing worked. Here is what I did.
STEPS
Worked ~~> Added gem 'friendly_id', '~> 5.1.0'
to my Gemfile and ran bundle install
Worked ~~> ran rails g friendly_id
$ rails g friendly_id
create db/migrate/20180404164213_create_friendly_id_slugs.rb
create config/initializers/friendly_id.rb
Fixed Migration File ~~> Changed class AddSlugToPosts < ActiveRecord::Migration
to class AddSlugToPosts < ActiveRecord::Migration[5.1]
Worked ~~> ran rake db:migrate
$ rake db:migrate
== 20180404164213 CreateFriendlyIdSlugs: migrating ============================
-- create_table(:friendly_id_slugs)
-> 0.0246s
-- add_index(:friendly_id_slugs, :sluggable_id)
-> 0.0048s
-- add_index(:friendly_id_slugs, [:slug, :sluggable_type])
-> 0.0035s
-- add_index(:friendly_id_slugs, [:slug, :sluggable_type, :scope], {:unique=>true})
-> 0.0031s
-- add_index(:friendly_id_slugs, :sluggable_type)
-> 0.0029s
== 20180404164213 CreateFriendlyIdSlugs: migrated (0.0392s) ===================
Worked ~~> ran rails g migration add_slug_to_posts slug:string:uniq
and then ran rake db:migrate
$ rails g migration add_slug_to_posts slug:string:uniq
invoke active_record
create db/migrate/20180404164731_add_slug_to_posts.rb
$ rake db:migrate
== 20180404164731 AddSlugToPosts: migrating ===================================
-- add_column(:posts, :slug, :string)
-> 0.0078s
-- add_index(:posts, :slug, {:unique=>true})
-> 0.0042s
== 20180404164731 AddSlugToPosts: migrated (0.0123s) ==========================
Updated posts_controller.rb
~~> From:
def set_post
@post = Post.find(params[:id])
end
To:
def set_post
@post = Post.friendly.find(params[:id])
end
Updated post.rb
~~> From:
class Post < ApplicationRecord
belongs_to :member
Status = ['Draft', 'Under Review', 'Edits Needed', 'Completed']
validates_with FeaturedValidator
end
To:
class Post < ApplicationRecord
extend FriendlyID
friendly_id :title, use: :slugged
belongs_to :member
Status = ['Draft', 'Under Review', 'Edits Needed', 'Completed']
validates_with FeaturedValidator
end
ERROR ~~> ran rails c
to open the console to update previous posts to have a slug. ran Post.find_each(&:save)
and got this error.
2.5.0 :001 > Post.find_each(&:save)
Traceback (most recent call last):
3: from (irb):1
2: from app/models/post.rb:1:in `<top (required)>'
1: from app/models/post.rb:8:in `<class:Post>'
NameError (uninitialized constant Post::FriendlyID)
Researched and quit console and tried spring stop
and the result stated that spring wasn't running. I then I tried the following and restarted the console each time before trying Post.find_each(&:save)
I tried changing extend FriendlyID
to extend ::FriendlyID
.
ERROR : NameError (uninitialized constant FriendlyID)
I tried adding require "friendly_id"
at the beginning.
ERROR : NameError (uninitialized constant Post::FriendlyID)
And I tried both together.
ERROR : NameError (uninitialized constant FriendlyID)
I then saw somewhere that the require: "friendly_id"
is supposed to be in the Gemfile and not the model. So, I deleted it there and added it to the Gemfile like so: gem 'friendly_id', '~> 5.1.0', require: "friendly_id"
ERROR : NameError (uninitialized constant Post::FriendlyID)
Any ideas would be greatly appreciated.
Solution
As I was writing this out and looking at my code, I realized that my error was due to a typo. I typed extend FriendlyID
instead of extend FriendlyId
. Notice the final 'd' is suppose to be lowercase, not uppercase. I will post an answer with my final code that works for anyone else that is having this issue.
As mentioned at the end of my question. My issue was that I had a typo when calling to extend 'FriendlyId' in my model. I capitalized the final 'd'. Here is my final code that works.
Gemfile
# Friendly URLs
gem 'friendly_id', '~> 5.1.0', require: "friendly_id"
app/models/post.rb
class Post < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
belongs_to :member
Status = ['Draft', 'Under Review', 'Edits Needed', 'Completed']
validates_with FeaturedValidator
end
app/controllers/post_controller.rb ~~ Changed def set_post
to
def set_post
@post = Post.friendly.find(params[:id])
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.