1

For my rails application, I am trying to create a random permalink for my users so that it is not localhost:3000/users/:id, but rather, it is localhost:3000/users/permalink.

I have followed the post made here: how to make ID a random 8 digit alphanumeric in rails?

Following the post, I have been able to create the random permalink column and pages for my users, but have not been able to get the sub-pages to work. For my users, I currently have sub-pages: Followers, etc.

Question: The pages are currently routed to localhost:3000/users/:id/followers, etc. But does somebody know how to fix routes.rb so that I can also route these pages to localhost:3000/users/permalink/followers, etc.

routes.rb

match 'users/:permalink' => 'users#show', :as => "show_user"

resources :users do
  member do
    get :followers
  end
end

user.rb

attr_accessible :permalink

before_create :make_it_permalink

def make_it_permalink
  self.permalink = SecureRandom.base64(8)
end

users_controller.rb

def show
  @user = User.find_by_permalink(params[:permalink])
end

def followers
  @title = "Followers"
  @user = User.find(params[:id])
  @users = @user.followers.page(params[:page]).per_page(5)
  render 'show_follow'
end

users/_header.html.erb

<%= render 'users/followerstats' %>

users/_followerstats.html.erb

<a href = "<%= followers_user_path(@user) %>">
    My Followers ( <%= @user.followers.count %> )
</a>

users/show_follow.html.erb

<div class = "container">
  <%= render 'header' %>
  <% provide(:title, @title) %>
  <div class="row">
    <div class="span12"> 
      <h4><%= @title %></h4>
      <% if @users.any? %>
        <ul class="users">
          <%= render @users %>
        </ul>
        <%= will_paginate %>
      <% end %>
   /div>
</div>
Community
  • 1
  • 1
spl
  • 609
  • 2
  • 9
  • 20

2 Answers2

1

I got it to work by adding the following:

routes.rb

match 'users/:permalink/followers' => 'users#followers', :as => "followers_user"

users_controller.rb

def followers
    @title = "Followers"
    @user = User.find_by_permalink(params[:permalink])
    @users = @user.followers.page(params[:page]).per_page(5)
    render 'show_follow'
end

users/_followerstats.html.erb

<a href = "<%= followers_user_path(@user.permalink) %>">
   My Followers ( <%= @user.followers.count %> )
</a>

*****UPDATE:**

Based on suggestion of @Benjamin Sinclaire and post here (Best way to create unique token in Rails?), I fixed up the make_it_permalink in user.rb:

def make_it_permalink
    loop do
      # this can create permalink with random 8 digit alphanumeric
      self.permalink = SecureRandom.urlsafe_base64(8)
      break self.permalink unless User.where(permalink: self.permalink).exists?
    end
end
Community
  • 1
  • 1
spl
  • 609
  • 2
  • 9
  • 20
0

Ok you found it while I was writing :)

Also be careful with your make_it_permalink function. There is a tiny chance that 2 users get the same permalink with your code. I suggest you to change it to:

def make_it_permalink
  begin
    self.permalink = SecureRandom.base64(8)
  end while User.exists?(:permalink => self.permalink)
end
Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
  • I updated to what you said and get the error: "ActiveRecord::StatementInvalid, SQLite3::SQLException: no such column: users.Z1Vxcc9/GGY=: SELECT 1 AS one FROM "users" WHERE "users"."Z1Vxcc9/GGY=" = 'Z1Vxcc9/GGY=' LIMIT 1" – spl May 06 '13 at 04:21
  • Thanks for the update, I updated my code (line 4) it should work now – Benjamin Bouchet May 09 '13 at 02:50