我第一次尝试使用PG hstore。
我的表单没有保存hstore数据。 控制台读数显示"数据" => {"充电器" =>""}}。我认为它应该是"数据" => {"充电器" =>" 6.6v"}}
这是HTML检查:
这是控制台:
这是表格:
<div class='clearfix attributeContainer'>
<label class="label" for="vehicle_data">Data Attributes</label>
<div class='row'>
<p class='col-md-3 underline'>Name</p>
<p class='col-md-3 underline'>Value</p>
</div>
<%= f.fields_for :data, @vehicle.data do |d| %>
<% @vehicle.data.try(:each) do |key, value| %>
<div class="row">
<p class='col-md-3'>
<%= text_field_tag key, key, :class => 'text_field dynamicAttributeName' %>
</p>
<p class='col-md-3'>
<%= d.text_field key, :class => 'text_field', :value => value %>
</p>
<p class='col-md-1'>
<a herf='#' class='btn removeRow'>X</a>
</p>
</div>
<% end %>
<%- end -%>
<div class='row attributeTemplate <%= 'hide' if @vehicle.data.present? %>'>
<p class='col-md-3'>
<input class='text_field dynamicAttributeName' id='' name='' placeholder='New Attribute name' size='30' type='text' />
</p>
<p class='col-md-3'><input class='text_field' id='bar' name='' placeholder='value' size='30' type='text' /></p>
<p class='col-md-1'><a herf='#' class='btn removeRow'>X</a></p>
</div>
<%= link_to 'Add Attribute', '#', :class => 'btn addAttribute btn-success' %>
</div>
这是咖啡脚本:
$(".attributeContainer").delegate ".dynamicAttributeName", "keyup", (event) ->
nameElem = $(this)
valueElem = nameElem.closest(".row").children("p").children(".text_field")
value = nameElem.val().toLowerCase()
valueElem.attr "id", "vehicle_data_" + value
valueElem.attr "name", "vehicle[data][" + value + "]"
valueElem.attr "placeholder", "value for " + value
return
$(".attributeContainer").delegate ".removeRow", "click", ->
console.log $(this).closest(".row")
$(this).closest(".row").html ""
return
$(".addAttribute").on "click", ->
contents = "<div class='row'>" + $(".attributeTemplate").html() + "</div>"
$(this).before contents
return
UPDATE1
我决定尝试从Rails控制台添加记录并收到错误:
2.0.0p353 :015 > c = Vehicle.create(name: “Test”, data: { battery: “LION”, charger: “6v” })
NameError: undefined local variable or method `“Test”' for main:Object
from (irb):15
UDPATE2
我最终做出了这样的改变:
valueElem = nameElem.closest(".row").children("p:nth-child(2)").children(".text_field")