Location field in rails

314 Views Asked by At

How do I create a location field for my form in rails? I want the user to be able to create a post that has an address, I already have the title and description fields but the location field I am having trouble with. I need to use these parameters (I'm confused where to put these in the form):

    params.require(:location).permit(:name, :formatted_address, :city, :state,
                                     :zip, :latitude, :longitude,
                                     :google_location_id)
  end

#and these are the fields I already have
            <p>
              <%= form.label :title %> <br>
              <%= form.text_field :title, class: 'form-control' %>
            </p>
            <p>
              <%= form.label :details %> <br>
              <%= form.text_area :details, :cols => "10", :rows => "7", class: 'form-control' %>
            </p>
1

There are 1 best solutions below

0
On

since you using bootstrap, you can just use text_field, below is sample if user want to put manually their coordinate

you can create with rails generate migration AddGeocodeToLocation, and the type of field is float

add_column :locations, :latitude,  :float, default: 0
add_column :locations, :longitude, :float, default: 0

and below is sample of form

<div class="row form-group">
  <%= f.label "Latitude", :class => 'col-form-label col-sm-3' %>
  <div class="col-sm-3">
    <%= f.text_field :latitude, class: "form-control" %> 
  </div>
  <%= f.label "Longitude", :class => 'col-form-label col-sm-3' %>
  <div class="col-sm-3">
    <%= f.text_field :longitude, class: "form-control" %>
  </div>
</div>