地理编码器不保存表中的任何信息

时间:2017-10-17 10:11:10

标签: ruby-on-rails geocoding

我已经安装了geocoder gem但是当我在表单中键入地址时它不会保存表中的任何参数,它只是一个空对象。有什么东西我不见了?

位置模型:

class Location < ApplicationRecord
attr_accessor :postcode, :longitude, :latitude
geocoded_by :postcode
after_validation :geocode, :if => :postcode_changed?

end

位置控制器:

    def create
      @location = Location.new(location_params)
       if @location.save
         redirect_to message_path(current_user.id)
         flash[:success] = "Success"
       else
         redirect_to root_path
         flash[:danger] = "failed"
       end
    end

    private

    def location_params
      params.require(:location).permit(:postcode, :user_id)
    end

以下是表格:

    <%= form_for @location, :url => locations_path do |f| %>
      <p>
        <%= f.label "postcode" %><br />
        <%= f.text_field :postcode %>
        <%= f.hidden_field :user_id, :value => current_user.id %>
      </p>
      <p><%= f.submit %></p>
    <% end %>

这是我的架构:

      create_table "locations", force: :cascade do |t|
        t.string "postcode"
        t.float "longitude"
        t.float "latitude"
        t.bigint "user_id"
        t.datetime "created_at", null: false
        t.datetime "updated_at", null: false
        t.index ["user_id"], name: "index_locations_on_user_id"
      end

所以我的想法是,用户可以在他的个人资料页面上的表格中输入他的邮政编码,他的位置会显示给所有其他用户看。

我可能没有理解它的概念,所以任何帮助或指导都会非常感激。感谢

哦,这是保存后的表格条目:

irb(main):011:0> Location.last
Location Load (0.4ms)  SELECT  "locations".* FROM "locations" ORDER BY "locations"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<Location id: 10, postcode: nil, longitude: nil, latitude: nil, 
user_id: 9, created_at: "2017-10-17 09:40:43", updated_at: "2017-10-17 09:40:43">

0 个答案:

没有答案