*_path method in rails


#1

Quick question about redirects here.

How does rails create the *_path methods?

eg.

# app/controllers/users_controller.rb
def create
  @user = User.new(username: params[:username], email: params[:email], password: params[:password])

  if @user.save
    redirect_to new_user_path
  else
    render :new
  end
end

in the above code where does new_user_path come from? I created a User model and a Users controller. What is the naming syntax behind this *_path method and what action produces it?


#2

Hey @babahyaga

When you define a route in the routes.rb file such as resources :users it takes that and creates default paths based on assumed routing paths.

As for where that’s actually handled in rails it’s here https://api.rubyonrails.org/classes/ActionDispatch/Routing.html


#3

you’re the best.