Henry Lee
Henry Lee

Reputation: 831

Rails 4 - Automatically fill in IP address location on the locations input field using Geocoder

I have this form:

= simple_form_for @post, validate: true do |f| 
            .edit-form
                = f.input :title
                = f.input :location
                = f.input :price
                = f.button :submit

The input form obviously is a blank text box that people put in their location.

enter image description here

I currently have Geocoder in my app and what I would like to do is pre-fill the location text box with the location of the user. Doing this requires this line of code:

= request.location.city
= request.location.state

I want to prefill this in my location text box. I tried doing something like this:

= f.input :location, value: (#{request.location.city}, #{request.location.state})

The end product should have the info filled in automatically and look like this:

enter image description here

This did not work. How do I insert it to accomplish this goal?


Upvotes: 0

Views: 316

Answers (2)

Loi Huynh
Loi Huynh

Reputation: 448

The other answers are wrong. The correct way to do this is:

= f.input :location, input_html: { value: "#{request.location.city}, #{request.location.state}"}

It is necessary to include the

input_html: { } 

If you want to add any additional HTML attributes.

Upvotes: 1

Dan Barbarito
Dan Barbarito

Reputation: 480

Instead of putting parenthesis around the value, use quotes to make it value: "#{request.location.city}, #{request.location.state}".

Upvotes: 0

Related Questions