SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: Clean URL's

  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Clean URL's

    I am working a project where I would like to make the url's clean, meaning, not showing the user id. I have been follow Patrick Lenz's book on how to do this without any success. No matter what I put, the route map.connect ':controller/:action/:id' allows gets executed. If I take it out and place my own at the very top, the whole things crashes. Is there another way or a better way to do this? Thanks,

    ~TRS

  2. #2
    SitePoint Member swatij's Avatar
    Join Date
    Jul 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    are you working with any framework?

    I am not sure what exactly you want but you can do it by using mod rewrite (.htacces) file.

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I want is to have the url read something like:

    http://www.domain.com/controller/action/user_name

    Or something close to that. I don't like the fact that the user id is displayed in the url, plus it is a bit of a security risk issue since in the end my form needs to not allow users to switch from one user profile to another simply by changing the user id in the url. Granted, there is more to be done before that happens, but this is just one thing that I have struggling with now for a couple of days. Thanks,

    ~TRS

  4. #4
    SitePoint Member swatij's Avatar
    Join Date
    Jul 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    here you need .htaccess file with mod mod_rewrite rules to create such browser friendly urls.

    Go through the following links it will help you in understanding & creating .htaccess file and Mod_rewrite rules.

    http://httpd.apache.org/docs/1.3/howto/htaccess.html

    http://www.yourhtmlsource.com/sitema...rewriting.html

    http://www.syndk8.com/mod_rewrite_tutorial_and_help.php

    http://www.php-learn-it.com/tutorial...atic_urls.html

    Go through the links, create your .htaccess.
    If you face any problem with your .htaccess file feel free to ask.

    All the best.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Miami, FL
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do something similar (except I set it to http://domain.com/screen_name) and what I did was:

    Code Ruby:
    map.profile ':screen_name', :controller => 'profile', :action => 'show'

    Something like

    Code Ruby:
    map.connect 'whatever/youwant/:user_name', :controller => 'yourcontroller', :action => 'youraction'

    Should work for you. You can make the URL's friendly without modifying .htaccess using routes.

    As far as securing the form from someone just replacing the user ID from one to another, it would work out the same way with them replacing the user name (you knew that already.) The easiest way to do this is to create a before_filter that checks to see whether the user that's logged in is the same as the user name being edited (or whatever action you're doing.) Adding a logged_in_user method to your application helper is very useful, so you can refer to that.

    Code Ruby:
    HELPER::
    ----------
    module ApplicationHelper
     
      def logged_in_user
        User.find(session[:user_id])
      end
     
    end
     
    -------------------------------------
     
    CONTROLLER::
    ----------------
    class MyController < ApplicationController
     
      before_filter :check_privileges
     
      def check_privileges
        @user = User.find(params[:user_id])
        if logged_in_user.id == @user.id
          return true
        else
          flash[:notice] = "You can't do that!"
          redirect_to whatever_url
        end
      end

    That'll work with ID's, just replace that with whatever you user name DB info is and you're good to go.
    Last edited by realmadrid2727; Aug 9, 2007 at 03:34. Reason: Posted prematurely

  6. #6
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The part of the my form about not allowing users to switch to another users form simply by changing the id in the browser works, but the clean URL part still nada. And what's real funny is the fact that I can put almost anything in my routes,rb file and it's almost as if its ignoring it completely. Thanks for your help though, I'll keep trying,

    ~TRS

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Miami, FL
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the contents of your routes.rb file exactly?

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    map.profile 'users/:screen_name', :controller => 'users', :action => 'show'
    
    # controller:
    
    def show
      @user = User.find_by_screen_name(params[:screen_name])
    end

  9. #9
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ActionController::Routing::Routes.draw do |map|
    # The priority is based upon order of creation: first created -> highest priority.

    # Sample of regular route:
    # map.connect 'products/:id', :controller => 'catalog', :action => 'view'
    # Keep in mind you can assign values other than :controller and :action

    # Sample of named route:
    # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
    # This route can be invoked with purchase_urlid => product.id)

    # You can have the root of your site routed by hooking up ''
    # -- just remember to delete public/index.html.
    # map.connect '', :controller => "welcome"

    # Allow downloading Web Service WSDL as a file with an extension
    # instead of a file named 'wsdl'
    #map.connect ':controller/service.wsdl', :action => 'wsdl'

    # Install the default route as the lowest priority.
    #
    map.connect ':controller/:action/:id'
    map.connect '', :controller=>"encore"
    end

    For the application that I'm working on, the base controller is enroll. I thought taht I should try something like map.enroll, but that didn't seem to work either.

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, what do you want your urls to look like?

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Miami, FL
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You haven't really added anything to the routes file though. Except that one line way at the bottom.

  12. #12
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by realmadrid2727 View Post

    Code Ruby:
    HELPER::
    ----------
    module ApplicationHelper
     
      def logged_in_user
        User.find(session[:user_id])
      end
     
    end
    I strongly advise not to write helpers like the above - your data access should be confined to your controllers, not your view or view helpers.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Miami, FL
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Really? I mean, you're right, it makes sense... separate logic from presentation, but I got that straight from a Rails book. So then I guess application controller.

  14. #14
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which book was that?

  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Miami, FL
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RailsSpace.

    I just checked the source code and there's a comment over the method saying "Never used in book." Still though, it's in the public source code so I knew I wasn't making it up


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •