【问题标题】:Location field in rails导轨中的位置字段
【发布时间】:2020-01-30 09:24:07
【问题描述】:

如何在 Rails 中为我的表单创建位置字段?我希望用户能够创建一个有地址的帖子,我已经有了标题和描述字段,但是我遇到了位置字段。我需要使用这些参数(我很困惑将它们放在表单中的哪个位置):

    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>

【问题讨论】:

    标签: ruby-on-rails forms geolocation location


    【解决方案1】:

    由于你使用引导程序,你可以只使用 text_field,如果用户想手动放置他们的坐标,下面是示例

    可以用rails创建生成迁移AddGeocodeToLocation,字段类型为float

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

    以下是表格示例

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多