Getting parameters from url in Ruby on Rails

Posted on 2009-04-25
Last Modified: 2013-11-13

I am a newbie to Ruby on Rails. I am doing an application where users can login and post messages to each other. I wish to be able to provide a search functionality (sort of) where in If somebody types in username along with the url, he would be able to see the messages posted by that user.

This would be my home url:

if somebody types

He should be able to see the postings by the user. Thank you.

Question by:sreekanthvasireddy
    LVL 12

    Expert Comment

    The quickest answer is to add a special-case route in 'config/routes.rb'.  In this particular case, you would probably want a line like the following (above the two likely already present):
      map.connect 'main/home/:username'
    The value "SREEKANTH", in your example, would then be accessed inside the 'home' action as 'params(:username)'.

    With the first "main/home" URL, since nothing was passed in the 3rd location, it would be handled by the ":controller/:action" line in the routes.rb file.

    You can also look at the other examples in the routes.rb file to see if another option would suit you better.
    LVL 1

    Author Comment

    Hai again cminear,

    Thanks for your reply.

    I added this line in the routes.rb.
    map.connect 'main/home/:username' : controller => 'main_controller'

    I added the following code in 'home' in 'main_controller'

    def home
            @user = User.find_by_username(params[:username])
            @message = Message.find(:all, :conditions => ["user_id = ?",])

    But I got this error.
    Routing Error
    No route matches "/main/home/Sreekanth" with {:method=>:get}


    Processing ApplicationController#index (for at 2009-04-27 09:34:14
    ) [GET]

    ActionController::RoutingError (No route matches "/main/home/Sreekanth" with {:m
      c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
      c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
      c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
      c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
      c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
      c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
      c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
      c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
      c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
      c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'

    Rendering rescues/layout (not_found)

    LVL 12

    Accepted Solution

    Make sure you restart the server.  Unlike controller and view changes made that are picked up immediately by the development mode with the server, the routes are only parsed at startup.

    As for your code displayed for the 'home' action, you will need to also handle the 'main/home' case, which would have nil for the 'params[:username]' variable.  Consider something like the following:
    def home
      if params[:username].blank?
        @user = nil
        @message = Message.find(:all)
        @user = User.find_by_username(params[:username])
        @message = Message.find(:all, :conditions => ["user_id = ?",])

    Open in new window

    LVL 1

    Author Comment

    Haaai cminear,

    It worked with what you said. Thank you very much for your help. I really appreciate you talking time to answer. Thanks

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
    Article by: narshlob
    If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now