Masatoshi Nishiguchi
Software Engineer

Custom routes in Rails

rails routes

You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional routes that apply to the collection or individual members of the collection.

Rails Guides

config/routes.rb

Rails.application.routes.draw do
  ...
  resources :songs do
    member do
      post 'add_favorite'
      delete 'remove_favorite'
    end
  end
  ...
end

app/controllers/songs_controller.rb

class SongsController < ApplicationController
  ...
  def add_favorite
    @song = Song.find(params[:id])
    @artist = @song.artist
    @song.favorites.create(user: current_user)
    redirect_to artist_path(@artist)
  end

  def remove_favorite
    @song = Song.find(params[:id])
    @artist = @song.artist
    @song.favorites.where(user: current_user).destroy_all
    redirect_to artist_path(@artist)
  end
  ...
$ rake routes | grep favorite
    add_favorite_song POST   /songs/:id/add_favorite(.:format)             songs#add_favorite
    remove_favorite_song DELETE /songs/:id/remove_favorite(.:format)          songs#remove_favorite

References