*_path method in rails


Quick question about redirects here.

How does rails create the *_path methods?


# 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
    render :new

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?


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


you’re the best.