私はRailsの4.2.6があり、2.3をルビー、ルーティングエラー初期化されていない一定のFavoriteSongsController
:
Routing Error uninitialized constant FavoriteSongsController
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
routes.rbを
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
私は様々なtututorialsを見て、数多くの提案に従ってもできるように思えてきましたいけないの
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 %>
自分で問題を解決する
'FavoriteSongsController'は' app/controllers/favorite_songs_controller.rb'で定義されていますか?アルファベット順にソートされたときに最初にコントローラが定義されることはありますか? –
'favorite_songs_Controller.rb'ファイルの名前は削除してください。 –
@ Anthony E:申し訳ありませんが、私は理解できませんでしたか? FavoriteSongsControllerが定義されています。アルファベット順の並べ替えとは何か – anouar