我按照此Rails 4 Unpermitted Parameters for Array但它不适用于我。这里是我的模型Rails 4坐标数组的未经允许的参数
class Building
include Mongoid::Document
include Mongoid::Spacial::Document
field :address, :type => String
field :location, :type => Array, spacial: {lng: :longitude, lat: :latitude, return_array: true }
end
在控制器
def update
ret = building_params.has_key?(:address) ? building_address_path(@building) : edit_building_path(@building)
respond_to do |format|
if @building.update(building_params)
format.html { redirect_to ret, notice: 'Building was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @building.errors, status: :unprocessable_entity }
end
end
end
def building_params
params.require(:building).permit(:address, :location => [])
end
HTML表单
<input id="building_location" name="building[location]" type="text" value="[112.70665740967, -7.26961135864]" />
服务器日志
Started PATCH "/id/buildings/52fb014862696b06ad100000" for 192.168.12.211 at 2014-02-13 14:08:24 +0700
Processing by BuildingsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"j/n8Sv1CmEf5LKQMjNb7iQndJjDzWqkxsGrVIdS6i54=", "building"=>{"location"=>"[112.75631079999994,-7.272195699999999]", "address"=>"Jalan Dharmawangsa"}, "commit"=>"Simpan", "locale"=>"id", "id"=>"52fb014862696b06ad100000"}
MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} runtime: 6.4688ms
MOPED: 127.0.0.1:27017 QUERY database=development collection=buildings selector={"_id"=>BSON::ObjectId('52fb014862696b06ad100000')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 8.3167ms
MOPED: 127.0.0.1:27017 QUERY database=development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('52fb013b62696b06ad000000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 9.1527ms
Unpermitted parameters: location
default_url_options is passed options: {}
Unpermitted parameters: location
Redirected to http://bikinibottom:3000/id/buildings/52fb014862696b06ad100000/address
Completed 302 Found in 1280ms
编辑:
我添加此到模型
def location=(loc)
location = eval(loc)
end
没有错误可言,但位置不保存
你在哪里调用'building_params'?此外,你传递一个字符串,但允许一个数组。 – sevenseacat
它应该只是'params.require(:building).permit(:address,:location)' –
@sevenseacat在更新操作中,我更新了我的问题。我不知道如何将它解析为模型 – spondbob