この動画を参考にuserにひもづくsongsを「人気順」「新着順」で並べ替えたく、scopeを使い以下のように実装しましたが、songs/like,songs/latestに飛ぼうとするとTemplate is missingのエラーがでます。
class Song < ActiveRecord::Base acts_as_votable belongs_to :user has_many :comments, :dependent => :destroy validates :title, {presence: true, length: {maximum: 140}} scope :like, ->{order(:cached_votes_up => :desc)} scope :latest, ->{order("created_at desc")} end
resources :comments devise_for :users resources :users, :only => [:index, :show ,:like] do member do get 'like', to: "users#like" get 'good', to: "users#good" end resources :comments resources :songs end resources :songs do collection do get :like get :latest end member do put "like", to: "songs#upvote" end resources :comments do member do put "good", to: "comments#upvote" end end end root to: "songs#index"
class SongsController < ApplicationController before_action :set_song, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, except: [:index, :show] # GET /songs # GET /songs.json def index if params[:user_id] @user = User.find(params[:user_id]) @songs = @user.songs# ここでアソシエーションが生きる else @songs = Song.all end end def like @songs = Song.all.like render action:index end def latest @songs = Song.all.latest render action:index end
まず、コントローラ内の記述についてなのですが、
「likeアクションに行ったら、indexアクション内の@songs = Song.allが@songs = Song.all.likeに置き換わる」という理解でよろしいでしょうか?
そして、もう一つの質問なのですが、なぜmissing templateになってしまうのでしょうか?
index.html.erbのビューにいき、無事並び替えられるようにするにはどうしたらいいのでしょうか?
あなたの回答
tips
プレビュー