简体   繁体   English

Rails:5无法使用Geocoder Gem按邮编/邮政编码显示搜索

[英]Rails:5 Unable to display search by postcode/zipcode using the Geocoder Gem

Building a small rails app that can search by postcode(zipcode) and radius, so it responds with contractors that are within that radius/postcode. 构建一个可以按邮政编码(邮政编码)和半径搜索的小型Rails应用程序,以便它响应该半径/邮政编码内的承包商。

i can do Location.near("M20 2WZ", 15) in the rails console which gives me the relevant info. 我可以在rails控制台中执行Location.near(“ M20 2WZ”,15),这会给我相关信息。

=> #<ActiveRecord::Relation [#<Location id: 12, company_name: "Redbridge Interiors Ltd", address: "16 Kennedy St", city: "manchester", postcode: "M2 4BY", latitude: 53.4799243, longitude: -2.2436708, created_at: "2017-07-11 11:39:03", updated_at: "2017-07-11 11:39:03">, #<Location id: 11, company_name: "Manchester Electrical Contractors Ltd", address: "Unit 9, The Schoolhouse, Second Ave", city: "Manchester", postcode: "M17 1DZ", latitude: 53.4646868, longitude: -2.3097547, created_at: "2017-07-11 11:36:57", updated_at: "2017-07-11 11:36:57">, #<Location id: 10, company_name: "J Hopkins (Contractors) Ltd", address: "Monde Trading Estate, Westinghouse Rd", city: "manchester", postcode: "M17 1LP", latitude: 53.4658142, longitude: -2.3278817, created_at: "2017-07-11 11:35:40", updated_at: "2017-07-11 11:35:40">]>

When i implement this in the app i get a url http://localhost:3000/locations?utf8=%E2%9C%93&search=m20+2wz but get no info/data/locations 当我在应用程序中实现此功能时,我得到一个URL http:// localhost:3000 / locations?utf8 =%E2%9C%93&search = m20 + 2wz但没有任何信息/数据/位置

schema.rb schema.rb

create_table "locations", force: :cascade do |t|
    t.string   "company_name"
    t.string   "address"
    t.string   "city"
    t.string   "postcode"
    t.float    "latitude"
    t.float    "longitude"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

location.rb location.rb

class Location < ApplicationRecord
  geocoded_by :full_address
  after_validation :geocode, :if => :address_changed?

  def full_address
   [address, city, postcode].compact.join(',')
  end
end

locations.controller.rb location.controller.rb

class LocationsController < ApplicationController
  def index
    @locations = Location.all
    if params[:search].present?
      @locations = Location.near(params[:search], 10, :order => :distance)
    else
      @locations = Location.all.order("created_at DESC")
    end
  end

  def new
    @location = Location.new
  end

  def show
    @location = Location.find(params[:id])
  end

  def create
    @location = Location.new(location_params)
    if @location.save
      flash[:success] = 'Place added!'
      redirect_to root_path
    else
      render 'new'
    end
  end

  private

  def location_params
    params.require(:location).permit(:company_name, :address, :city,
                                    :postcode, :latitude, :longitude)
  end
end

index.html.erb index.html.erb

<div class="container">
  <div class="starter-template">
    <h3>SubContractor Postcode search</h3>
      <p class="lead">Use this postcode search to quickly find subcontractors in your area</p>
      <p><%= form_tag locations_path, :method => :get do %>
        <%= text_field_tag :search, params[:search], placeholder: "e.g M20 2WZ" %>
        <%= submit_tag "Search Near", :name => nil %>
      </p>
    <% end %>

    </div>
</div>

routes.rb routes.rb

Rails.application.routes.draw do
  resources :locations, except: [:update, :edit, :destroy]
  root 'locations#index'
end

You are not displaying the locations in index.html.erb Add this in your index.html.erb 您没有在index.html.erb显示位置。将其添加到index.html.erb

<% @locations.each do |location| %>
<%= location.city %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM