Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I do not consider exposing user IDs to public as a security flaw, there should be other mechanisms for security. Maybe it is a "marketing security flaw" when visitors find out you do not have that million users they promise ;-)</p> <p>Anyway:</p> <p>To avoid IDs in urls at all you can use the user's login in all places. Make sure the login does not contain some special characters (<code>./\#?</code> etc.), that cause problems in routes (use a whitelist regex). Also login names may not be changed later, that can cause trouble if you have hard links/search engine entries to your pages.</p> <p>Example calls are <code>/users/Jeff</code> and <code>/users/Jeff/edit</code> instead of <code>/users/522047</code> and <code>/users/522047/edit</code>.</p> <p>In your user class you need to override the <code>to_param</code> to use the login for routes instead of the user's id. This way there is no need to replace anything in your routes file nor in helpers like <code>link_to @user</code>.</p> <pre><code>class User &lt; ActiveRecord::Base def to_param self.login end end </code></pre> <p>Then in every controller replace <code>User.find</code> by <code>User.find_by_login</code>:</p> <pre><code>class UsersController &lt; ApplicationController def show @user = User.find_by_login(params[:id]) end end </code></pre> <p>Or use a <code>before_filter</code> to replace the params before. For other controllers with nested resources use <code>params[:user_id]</code>:</p> <pre><code>class UsersController &lt; ApplicationController before_filter :get_id_from_login def show @user = User.find(params[:id]) end private # As users are not called by +id+ but by +login+ here is a function # that converts a params[:id] containing an alphanumeric login to a # params[:id] with a numeric id def get_id_from_login user = User.find_by_login(params[:id]) params[:id] = user.id unless user.nil? end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload