Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails URL Format

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
like image 566
user1658756 Avatar asked Nov 19 '12 13:11

user1658756


2 Answers

Rails uses the to_param method of an ActiveRecord object in order to resolve it into a URL.

Assuming you have a way to generate these unique ids (referring to it as IdGenerator) you can do the following:

1- Generate this id whenever you persist a Post object and save it to the database, let's say under the column url_id

class Post < ActiveRecord::Base
  before_create :generate_url_id
  def generate_url_id
    self.url_id = IdGenerator.generate_id
  end
end

2- Inside your Post model override the to_param method:

class Post < ActiveRecord::Base
  def to_param
    return url_id
  end
end

Now post_path(@post) will resolve to /posts/url_id

By the way, you can use SecureRandom.urlsafe_base64 or look here if you don't have an ID generator yet.

Read more on the documentation for to_param.

like image 72
Erez Rabih Avatar answered Oct 21 '22 02:10

Erez Rabih


I hope these two resources are going to help you :

  1. The gem , named obfuscate_id . It represents the ID in a format like :

    http://tennisfans.eu/products/4656446465

  2. Another gem - masked_id . It provides a similar functionality . You are in control with a format of the url creation , defining it in a class . Looking at the source it appears , that this gem uses a strategy of obfuscate_id gem .

like image 23
R Milushev Avatar answered Oct 21 '22 02:10

R Milushev