2016-05-16 71 views
0

我有Rails的4.2.6和Ruby 2.3,路由错误未初始化的常量FavoriteSongsController

继添加收藏到我现有的项目,因此用户可以喜欢的房源信息,但不能让过去的错误如下教程:

Routing Error uninitialized constant FavoriteSongsController 

screenshot

favorite_songs_Controller.rb

class FavoriteSongsController < ApplicationController 
    before_action :set_song 

    def create 
    if Favorite.create(favorited: @fav_song, user: current_user) 
     flash[:success] = t('favorite_song.success') 
     redirect_to :back 
    else 
     flash[:danger] = t('favorite_song.wrong') 
     redirect_to :back 
    end 
    end 

    def destroy 
    Favorite.where(favorited_id: @fav_song.id, user_id: current_user.id).first.destroy 
    flash[:info] = t('favorite_song.destroy') 
    redirect_to :back 
    end 

    private 

    def set_song 
    @fav_song = Song.find(params[:song_id] || params[:id]) 
    end 
end 

songs_controller.rb

class SongsController < ApplicationController 
    def show 
    @artist = Artist.friendly.find(params[:artist_id]) 
    @song = @artist.songs.find(params[:id]) 
    end 
end 

song.rb

class Song < ActiveRecord::Base 
    include PgSearch 


    belongs_to :artist 
    default_scope -> { order(:title) } 

    validates :artist_id, presence: true 
    validates :title,  presence: true, 
        length: { maximum: 140 } 


    has_attached_file :mp3, 
    :url => "/songs/:artist_slug/:song_slug_:hash.:extension" 
    validates_attachment :mp3, 
    :content_type => { :content_type => ["audio/mpeg", "audio/mp3"] }, 
    :file_name => { :matches => [/mp3\Z/] } 
    validates_attachment_presence :mp3 
    validates_attachment_size :mp3, { less_than: 15.megabytes } 

    Paperclip.interpolates :artist_slug do |attachment, style| 
    attachment.instance.artist.slug 
    end 

    Paperclip.interpolates :song_slug do |attachment, style| 
    attachment.instance.slug 
    end 

    Paperclip::Attachment.default_options.update({ 
    :hash_secret => "jHLi3fdrmHZQ8r9wxGZbyyGzc9BT8UAxOk2q6O1T1ut+pgmDqmtFIdaBuw8tkhAK0nhDMrCQYCMfOXiUH3R27zG22OHi0852jK93/TaiDtymwzPXZQOxhM6KR6aODhEK+LmqYG1uIGHvqfzD1BOh/R7JuvW2cf+0dT0V3hiFEzA=" 
    }) 

    pg_search_scope :search_by_title, 
        :against => :title, 
        :using => { 
         :trigram => { 
         :threshold => 0.1 
         } 
        } 

    def slug=(value) 
    if value.present? 
     write_attribute(:slug, value) 
     end 
    end 

end 

user.rb

class User < ActiveRecord::Base 
    authenticates_with_sorcery! 

    has_many :favorites 
    has_many :favorite_songs, through: :favorites, source: :favorited, source_type: 'Song' 

    validates :email,     presence: true, length: { maximum: 255 }, 
             email_format: { message: I18n.t('signup.email_invalid') }, 
             uniqueness: { case_sensitive: false } 

    validates :password,     presence: true, confirmation: true, length: { minimum: 6} 


end 

favorite.rb

class Favorite < ActiveRecord::Base 
    belongs_to :favorited, polymorphic: true 
    belongs_to :user 
end 

routes.rb中

Rails.application.routes.draw do 

    mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 
    root 'home#index' 
    get 'search', to: 'search#search' 


    resources :users, only: [:index, :show, :edit, :update] 
    get '/signup', to: 'users#new' 
    post '/signup', to: 'users#create' 


    get '/login', to: 'sessions#new' 
    post '/login', to: 'sessions#create' 
    get '/logout', to: 'sessions#destroy' 

    resources :reset_passwords, only: [:new, :create, :update, :edit] 


    resources :artists, :path => "/", only: [:show] do 
    resources :songs, :path => "/", only: [:show] 
    end 

    resources :favorite_songs, only: [:create, :destroy] 
end 

歌曲/ songs.html.erb

<%= render 'favorite_songs/fav' %> 

favorite_songs/_fav.html.erb

<% if logged_in? %> 
    <%- unless current_user.favorites.exists?(id: @song.id) -%> 
     <%= link_to 'Add to favorites', favorite_songs_path(song_id: @song), method: :post %> 
    <%- else -%> 
     <%= link_to 'Remove from favorites', favorite_song_path(@song), method: :delete %> 
    <%- end -%> 
<% end %> 

我看过各种tututorials,随后提出了许多建议,但不要似乎能自己解决问题。

+0

'app/controllers/favorite_songs_controller.rb'中是否定义了'FavoriteSongsController'?它是第一个按名称按字母顺序排序的控制器定义的机会吗? –

+2

'favorite_songs_Controller.rb'文件的名称应该是downcased,请修复它 –

+0

@ Anthony E:对不起,我不明白? FavoriteSongsController被定义。但如何按字母顺序排序 – anouar

回答

3

的名称为favorite_songs_Controller.rb文件以及在应用程序的其他文件/文件夹应downcased,所以请修复和/或其他文件了。您可以按照以下步骤从项目的根目录开始:

find app -type f |while read f; do i=$(tr "[A-Z]" "[a-z]" <<< $f); mv "$f" "$i" 2>/dev/null; done 
2

Your FavoriteSongsController在favorite_songs_Controller.rb。 Ruby将无法自动找到不符合命名约定的文件中的类。相反,您应该使用favorite_songs_controller.rb。请注意名称中的小写'C'controller。这就是导致问题的原因。改变这一点,你应该会很好。

相关问题