Rails 4坐标数组的未允许参数

时间:2014-02-13 07:15:20

标签: ruby-on-rails ruby-on-rails-4

我已遵循此Rails 4 Unpermitted Parameters for Array但它对我不起作用。这是我的模特

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

根本没有错误,但未保存位置

3 个答案:

答案 0 :(得分:0)

查看传递给控制器​​的params,似乎“location”属性作为字符串而不是数组传递,
  参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“j / n8Sv1CmEf5LKQMjNb7iQndJjDzWqkxsGrVIdS6i54 =”,“building”=&gt; {“location”=&gt;“[112.75631079999994,-7.272195699999999]”,“地址“=&gt;”Jalan Dharmawangsa“},”commit“=&gt;”Simpan“,”locale“=&gt;”id“,”id“=&gt;”52fb014862696b06ad100000“}

为了验证参数'building [location]'包含字符串数据的事实,打印params [:building] [:location]的类,它应该打印字符串

如果参数以字符串形式传递(如上面的参数传递中所示),则将代码更改为以下代码:

def building_params
  params.require(:building).permit(:address, :location)
 end

答案 1 :(得分:0)

我的建议是你应该将参数作为数组发送,如下面的代码所示:

<input id="building_location" name="building[location]" type="text" value=[112.70665740967, -7.26961135864] />

并将构建params代码重新转换回

def building_params
  params.require(:building).permit(:address, :location => [])
end

或者,如果你不能这样做,你必须解析params'建筑物[location]',如下面的代码所示

def building_params
  params[:building][:location] = params[:building][:location][1..-2].split(',') unless params[:building][:location].blank?
  params.require(:building).permit(:address, :location => [])
end

答案 2 :(得分:0)

[OP&#39的解决方案粘贴在下面作为答案]

我在这里找到了答案。正如您在我的服务器日志中看到的那样,有两个Unpermitted parameters: location,因为我在更新操作中调用了building_params两次(第2行和第4行)。

只要我将第2行更改为ret = params[:building].has_key?(:address) ...并应用@sanjiv的解决方案,就会出现问题。

输入值为"[112.75631079999994,-7.272195699999999]"字符串,基于@sanjiv的解决方案:

params[:building][:location][0] = "[112.75631079999994"
params[:building][:location][1] = "-7.272195699999999]"

请注意括号。然后我修理了它:

def building_params
  params[:building][:location] = eval(params[:building][:location])
  params.require(:building).permit(:address, :location => [])
end

eval()允许您从包含括号的字符串转换为数组。但为什么我不能这样做呢?

# controller
def building_params
  params.require(:building).permit(:address, :location)
end

# model
def location=( loc )
  location = eval(loc)
end

如果:location是数组,rails是否已在控制器中知道?服务器日志中没有错误消息