我有模特
class Building
include Mongoid::Document
include Geocoder::Model::Mongoid
field :address, :type => String, :default => ""
field :location, :type => Array, spacial: {lat: :latitude, lng: :longitude, return_array: true }
## Building index
index({location: "2d"})
def latitude
location[1]
end
def longitude
location[0]
end
def latitude=( lat )
location[1] = lat
end
def longitude=( lng )
location[0] = lng
end
end
视图中的此表单
= f.text_field :latitude
= f.text_field :longitude
这是控制器
...
def create
@building = Building.new(building_params)
respond_to do |format|
if @building.save
format.html { redirect_to @building, notice: 'Building was successfully created.' }
format.json { render action: 'show', status: :created, location: @building }
else
format.html { render action: 'new' }
format.json { render json: @building.errors, status: :unprocessable_entity }
end
end
end
private
def building_params
params.require(:building).permit(:address, :latitude, :longitude)
end
...
在表单中填充纬度和经度,但是当我更改值并保存它时,它不起作用,甚至在数据库中更改了像地址这样的其他字段。但它在控制台中完美运行
> b = Building.first
> b.latitude = -7.27094221115
> b.save
=> true
答案 0 :(得分:0)
我认为params
上的问题,来自您拥有string
纬度和经度的表单,但需要float
。