New Discussion

Notifications

You’re not receiving notifications from this thread.

Create Customer Url

2
Rails

Hello @chris and fellow members. I am new to rails and I was seeking help on how to create a url like "mydomain.com/5047315/projects/21789643 where is 5047315 the user account number ?

Regards,
Suleiman Nassor

Hey Suleiman,

something along those lines should work:

routes.rb

get "/:user_account_number/projects/:project_id", to: "projects#show"
# pay attention to the colons (:) within the url here. It signals to Rails that these are variable parts ("params") which can change.

projects_controller.rb

class ProjectsController < ApplicationController
  def show
    user = User.find_by(user_account_number: params[:user_account_number])

    if user.present?
      @project = user.projects.find(params[:project_id])
    else
      flash[:alert] = "No project found with ID: #{params[:project_id]}"
    end
  end
end

Regards,
Dom

Thanks Dom.

Join the discussion
Create an account Log in

Learning Ruby on Rails? Join our newsletter.

We won't send you spam. Unsubscribe at any time.