0

I am having the hash with following keys

{:action=>'index', :controller=>'users', :search=>'John'}

I want to build a complete request url using this hash keys. I tried using ActionDispatch::Routing::RouteSet class, but I couldn't able to get the url as expected.

I want to build the url like http://localhost:3000/users?search=John

How to get this request url?

Achaius
  • 5,904
  • 21
  • 65
  • 122

1 Answers1

2
url_for(hash)

in your case:

url_for(:action=>'index', :controller=>'users', :search=>'John')

returns /users?search=John host_name will be added automatically.

If you explicitly want to add host_name you can pass host: key in your hash

Muntasim
  • 6,689
  • 3
  • 46
  • 69
  • I was in the process of providing this answer, based on http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html, but `url_for` wasn't recognized when I typed that into the Rails console. After following the instructions in that doc about what to include, I was able to call the function, but it raised an error due to `request` not being defined. Any idea what was going on there? – Peter Alfvin Jun 24 '13 at 12:49
  • I guess its not about "testing in console". Did you get it working inside the app? – Muntasim Jun 24 '13 at 12:51
  • I'm not the OP and I'm not questioning whether it works in the app. Just thought you might know why I was having trouble getting it to work in the console. – Peter Alfvin Jun 24 '13 at 13:02
  • Actually I didnt try with console. 'll have a try – Muntasim Jun 24 '13 at 13:07