gpt4 book ai didi

ruby-on-rails - SongsController 中的 NameError#index 未初始化常量 Song::FriendlyId

转载 作者:行者123 更新时间:2023-12-04 03:41:33 25 4
gpt4 key购买 nike

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

我已经安装了 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

歌曲 Controller 片段

  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

最佳答案

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

bundle install

然后

exit // rails s (restart your server)

关于ruby-on-rails - SongsController 中的 NameError#index 未初始化常量 Song::FriendlyId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18175670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com