0

I have a website with a form for location on the front page. I want to auto-populate the form with values based on a URL string. How can I do this in rails?

For example, I would like the form to show with the value New York when the user arrives on the front page based on something like: website.com?city="New York"

How best to implement this in rails?

william tell
  • 4,352
  • 6
  • 23
  • 27

1 Answers1

2

First of all, you can get the current request url, everything is here : How do I get the current absolute URL in Ruby on Rails?

Then you just have to "parse" it :

<% city = request.fullpath.split('=').last %>

Will return "New York" with a url like website.com?city="New York" (Not sure about this one but maybe you can get it only with params[:city], I just don't think params is available in a view)

The rest depends on the structure of your form...

Community
  • 1
  • 1
Raindal
  • 3,227
  • 15
  • 14