Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:06:05+00:00 2026-06-15T17:06:05+00:00

I have a Ruby on Rails application where you can create ‘posts’. I started

  • 0

I have a Ruby on Rails application where you can create ‘posts’. I started of by using the scaffold generator to give generate the title which is a string and the body which is the content.

Each ‘post’ has a url of the id, for example /1, /2, /3, etc.

Is there a way to change that to a string of random characters, for example /49sl, /l9sl, etc?

Update

Here is what I have for the posts_controller.rb

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
   @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:06:06+00:00Added an answer on June 15, 2026 at 5:06 pm

    You can give your posts random URLs by following these 3 steps:

    1- In your model (Post.rb), generate a random string for each post before it is saved. For example,

    class Post < ActiveRecord::Base
      before_create :generate_url_id
      def generate_url_id
        self.url_id = SecureRandom.urlsafe_base64
      end
    end
    

    2- In your model (Post.rb), supply a to_param method to override Rails default URL generation. For example:

    class Post < ActiveRecord::Base
      def to_param
        self.url_id
      end
    end
    

    3- In your controller (PostsController.rb), use a dynamic finder to find your post by its random string. For instance,

    class PostsController < ApplicationController
      def show
        @post = Post.find_by_url_id(params[:id])
        ...
      end
    end
    

    I went ahead and put together a complete example and posted it to Github.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a blog application using Ruby on Rails which includes the ability
I have built a very simple blog application using Ruby on Rails. New to
I have a Rails 3.2.2 application using Ruby version 1.9.2. I have created basic
I have a Ruby on Rails application that allows for users to create a
I am using Ruby on Rails 3 and I have a monolithic application that
I have a problem running a Ruby on Rails application using Passenger. My application
I have a Ruby on Rails application that needs to find a home in
I have a ruby on rails application and it would be nice to see
I have Ruby on Rails 3.1 application and i use for Sphinx + Thinking
I have used geocoder in my ruby on rails application for a while but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.