[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting parameters from url in Ruby on Rails

Posted on 2009-04-25
4
Medium Priority
?
806 Views
Last Modified: 2013-11-13
Hai,

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:
http://localhost:3000/main/home

if somebody types
http://localhost:3000/main/home/SREEKANTH

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

0
Comment
Question by:sreekanthvasireddy
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:cminear
ID: 24238444
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.
0
 
LVL 1

Author Comment

by:sreekanthvasireddy
ID: 24238833
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 = ?", @user.id])
       
    end
<----------

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

THIS IS THE BACKEND ERROR I AM GETTING

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

ActionController::RoutingError (No route matches "/main/home/Sreekanth" with {:m
ethod=>:get}):
  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)


0
 
LVL 12

Accepted Solution

by:
cminear earned 2000 total points
ID: 24242401
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)
  else
    @user = User.find_by_username(params[:username])
    @message = Message.find(:all, :conditions => ["user_id = ?", @user.id])
  end
  ...
end

Open in new window

0
 
LVL 1

Author Comment

by:sreekanthvasireddy
ID: 24249272
Haaai cminear,

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 5 hours left to enroll

872 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