我有一个 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