简体   繁体   中英

Friendly_Id error, unintialized constant

I have a rails app that has Devise-created users that own 'items.' These items have show views and I wanted those show views to have more SEO-friendly URL's. I watched the railscast for the Friendly_Id gem, implemented, but when I go to create a new 'item', it gives me this error:

uninitialized constant Item::FriendlyId

When I attempt to click on an item, it gives me this error:

undefined method `key?' for nil:NilClass

I ran a bundle install. The gem is not in gem assets.

Here is my item model:

 1 class Item < ActiveRecord::Base                                                 
 2 #  include Tire::Model::Search                                                  
 3 #  include Tire::Model::Callbacks                                               
 4                                                                             
 5   extend FriendlyId                                                             
 6   friendly_id :title, use: :slugged                                                                                        
 7                                                                                 
 8   attr_accessible :content, :user_id, :title, :price, :image                    
 9   validates :content, :length => { :maximum => 140 }                            
10   belongs_to :user                                                              
11   delegate :email, :city, :state, to: :user                                     
12                                                                                 
13   def self.search(search)                                                       
14     if search                                                                   
15       where('title ILIKE ? OR content ILIKE ?', "%#{search}%", "%#{search}%")   
16     else                                                                        
17       scoped                                                                    
18     end                                                                         
19   end                                                                           
20                                                                                 
21   def location                                                                  
22     [city.to_s.camelcase, state.to_s.upcase].reject(&:blank?).join(", ")        
23   end                                                                           
24                                                                                 
25   has_attached_file :image, styles: {                                           
26     thumb: '100x100>',                                                          
27     square: '200x200#',                                                         
28     medium: '300x300>',                                                         
29     large: '600x600#'                                                           
30   }
31 end

here is my user model:

1 class User < ActiveRecord::Base                                                 
2   # Include default devise modules. Others available are:                       
3   # :token_authenticatable, :confirmable,                                       
4   # :lockable, :timeoutable and :omniauthable                                   
5   devise :database_authenticatable, :registerable,                              
6          :recoverable, :rememberable, :trackable, :validatable                  
7                                                                                 
8   # Setup accessible (or protected) attributes for your model                   
9   attr_accessible :email, :password, :password_confirmation, :remember_me, :username,           :city, :state, :phone
10   has_many :items                                                               
11                                                                                 
12   validates_presence_of :username                                               
13   validates_uniqueness_of :username                                             
14                                                                                 
15   def to_param                                                                  
16     username                                                                    
17   end                                                                                                                        
18                                                                                 
19   after_create :send_welcome_email                                              
20                                                                                 
21   private                                                                       
22                                                                                 
23   def send_welcome_email                                                        
24     UserMailer.welcome_email(self).deliver                                      
25   end                                                                           
26                                                                                 
27 end    

I had the same issue. I tried the :require => 'friendly_id' as well as as a server restart. It seems to have fixed the issue.

Thanks @StuartM and @fmendez

I ran into this issue as well. To resolve I wrote a rake task ...

require 'rake'
namespace :posts do
  desc "Save all posts"
  task :save => :environment do
    puts "--- Saving posts ---"
    Post.find_each(&:save)
    puts "All posts have been saved.\n"
  end
end

... executed rake posts:save from my_app/ directory and touch tmp/restart.txt to restart the web server.

Update : Just ran this on my production website. I have to use rake posts:save RAILS_ENV=production otherwise rake raised an exception that it could not find the database adapter.

This is what worked:

Migrate DB to include Slugs on Items, INDEXED Run Migration Rails Console: Item.find_each(&:save)

Then pushed to Heroku and did the same. Worked.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM