-4

I'm trying to understand an app in which it creates a link via link_to to new_user_session_path. In my controller, there is no new nor session. The generate link is users/sign_in which you can see here : [ListenUp][1]. Also, sign_in is not in the controller. My hunch is that it is some RoR magic.

The controller :

class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def show
    @user = User.find_by_permalink(params[:id])
    @songs = Song.where(user:  @user)
    @friendships = @user.all_friendships
  end
end

The routes

Rails.application.routes.draw do

devise_for :users
resources :users
resources :friendships
root 'pages#search'

resources :pages
resources :playlists
resources :songs

get 'search' => 'pages#search'
get 'search_results' => 'pages#search_results'


end

Part of the view that I'm trying to figure out :

<li><%= link_to "sign in", new_user_session_path %></li>
<li><%= link_to "sign up", new_user_registration_path %></li>

Thanks

[1]: http://listenup-songshare.herokuapp.com/
MusicAndCode
  • 870
  • 8
  • 22
  • 3
    [Obligatory comment about not linking to code and https://stackoverflow.com/help/mcve] Moving on, paths are typically defined by the user in routes.rb. In this case, new_user_session_path is defined by Devise, and so you don't see it in any of the usual places. It is possible to override these with your own routings, which you can read more about at https://github.com/plataformatec/devise/wiki/How-To:-Customize-routes-to-user-registration-pages and https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes. – Deem May 02 '17 at 20:06
  • Thankls for the information, I'll update it right away. – MusicAndCode May 02 '17 at 23:47

1 Answers1

1

Rails will automatically generate helpers for you based on your route names. See Rails Routing From the Outside In for this straight from the horses's mouth.

By Convention these helpers look something like ACTION NAME + CONTROLLER NAME + "path" (or "url").

Given this routes file, you might have a new_song_path generated for you.

In addition to this, gems you add to your Gemfile can also create additional routes. Here you see that new_user_session_path is not your code.

To list all the routes in your app, including those added by other gems, run rake routes. Usually I run rake routes > tmp/routes.txt, which saves the output to a file named tmp/routes.txt (if you're not hip to bash), and I refer to this file often when developing my Rails app.

RyanWilcox
  • 13,890
  • 1
  • 36
  • 60
  • Has seen in the comment of @Windmill, it comes from Devise. So I guess I should find a controller names UserSession with a new action. Thanks for your help. – MusicAndCode May 03 '17 at 01:45
  • A new action or an index method, yes (it depends... I think rake routes should tell you the method name too...) – RyanWilcox May 03 '17 at 01:48