3

我有一个 Rails 应用程序,它拥有拥有“项目”的 Devise 创建的用户。这些项目有显示视图,我希望这些显示视图具有更多对 SEO 友好的 URL。我观看了 Friendly_Id gem 的 railscast,实现了,但是当我去创建一个新的“项目”时,它给了我这个错误:

uninitialized constant Item::FriendlyId

当我尝试单击一个项目时,它给了我这个错误:

undefined method `key?' for nil:NilClass

我运行了捆绑安装。宝石不在宝石资产中。

这是我的项目模型:

 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

这是我的用户模型:

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    
4

3 回答 3

2

我也遇到了这个问题。为了解决我写了一个 rake 任务......

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

...rake posts:save从 my_app/ 目录执行并touch tmp/restart.txt重新启动 Web 服务器。

更新:刚刚在我的生产网站上运行了这个。我必须使用,rake posts:save RAILS_ENV=production否则 rake 引发了它找不到数据库适配器的异常。

于 2014-01-26T20:00:56.770 回答
2

我遇到过同样的问题。我尝试了:require => 'friendly_id'以及服务器重新启动。它似乎已经解决了这个问题。

谢谢@StuartM 和@fmendez

于 2013-05-31T22:14:43.583 回答
0

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.

于 2013-04-08T16:41:26.153 回答