All threads / Create Customer Url

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Create Customer Url

Suleiman Nassor asked in 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

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 46,687+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.