views:

215

answers:

1

Hi guys,

I'm using named routes in my ruby code. I come from the phpworld where you'll pass information using $_GET and $_POST. I was wondering if there's a way to put this into the routes.rb like this:

map.with_options :controller => 'test' do |m|
  m.someurl 'someurl?search=someterm', :action => 'index'
end

Currently it's returning can't convert Hash into String. Thanks!

Justin

+3  A: 

If you want to just use a Query String, you don't need to tell your route at all. The params object will contain any passed parameters.

map.with_options :controller => 'test' do |m|
  m.some_url 'someurl', :action => 'index'
end

Then when you use the helper method:

some_url_path(:search=> "someterm")

Will create the query string value for you.

However, if you want to pass a parameter to a controller, you can bind them in your route:

map.connect ':controller/:action/:id/:search' 

In your controller you can then access:

params[:search]

In your case this would up being something like:

map.with_options :controller => 'test' do |m|
  m.some_url 'someurl' :action => 'index'
end

The Rails Routing Guide provides an excellent overview on the subject.

Toby Hede