3

所以,我有一个应用程序,用户可以在其中进行大量的社交分享。因此,链接必须看起来很漂亮。

我已经安装了friendly_id gem

但似乎收到此错误:

NameError in SongsController#index
uninitialized constant Song::FriendlyId

class Song < ActiveRecord::Base

   extend FriendlyId
    friendly_id :title, use: :slugged

song.rb 片段

class Song < ActiveRecord::Base

   extend FriendlyId
    friendly_id :title, use: :slugged

歌曲控制器片段

  class SongsController < ApplicationController
  before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy, :vote_for_song]
  before_action :set_song, only: [:show, :edit, :update, :destroy, :vote_for_song]

  def index
    if params[:query].present? 
      @songs = Song.search(params)
      get_last_song
    elsif params[:genre]
      @songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
      get_last_song
    else      
      @songs = Song.order('id').order('plusminus desc nulls last').paginate(:page => params[:page], :per_page => 15) 
      #@songs = Song.tally.paginate(:page => params[:page], :per_page => 15)
      get_last_song
    end
  end
  def set_song
       @song = Song.friendly.find(params[:id])
     end

     def song_params
       params.require(:song).permit(:title, :artist, :url, :track, :user_id, :tag_list, :query, :genre, :genre_names, )
     end
  end

schema.rb 片段

  create_table "songs", force: true do |t|
    t.string   "title"
    t.string   "artist"
    t.text     "url"
    t.string   "track_file_name"
    t.string   "track_content_type"
    t.integer  "track_file_size"
    t.datetime "track_updated_at"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "plusminus"
    t.string   "slug"
  end

  add_index "songs", ["slug"], name: "index_songs_on_slug", unique: true, using: :btree



create_table "friendly_id_slugs", force: true do |t|
    t.string   "slug",                      null: false
    t.integer  "sluggable_id",              null: false
    t.string   "sluggable_type", limit: 40
    t.string   "scope"
    t.datetime "created_at"
  end
4

1 回答 1

3

问题已解决,每当您看到未初始化的常量时,通常意味着您必须运行

bundle install

接着

exit // rails s (restart your server)
于 2013-08-11T19:31:04.557 回答