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

Reply

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

Reply

Thanks Dom.

Reply
Join the discussion
Create an account Log in

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

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

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