anouar anouar - 6 months ago 13
Ruby Question

Routing Error uninitialized constant FavoriteSongsController

i have Rails 4.2.6 and ruby 2.3 ,

Following a tutorial for adding favorites to my existing project, whereby users can favorite property listings but can't get past the error below:

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/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 %>


I've watched various tututorials and followed numerous suggestions but dont seem to be able to solve the issue by myself.

Answer

A name for favorite_songs_Controller.rb file as well as other files in app/ folder should be downcased, so please fix it and/or other files too. You can do it as follows from root of your project:

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