Ruby on rails 使用嵌套形式创建两个模型

Ruby on rails 使用嵌套形式创建两个模型,ruby-on-rails,ruby,ruby-on-rails-3,nested-forms,Ruby On Rails,Ruby,Ruby On Rails 3,Nested Forms,我正在尝试创建一个嵌套的表单rails,其中将同时创建两个模型。该模型包括以下内容: 模型/事件 #Data attr_accessible :customer_id, :description, :locations_attributes #Relationship has_many :locations accepts_nested_attributes_for :locations, :allow_destroy => true 型号/地点 #Data attr_a

我正在尝试创建一个嵌套的表单rails,其中将同时创建两个模型。该模型包括以下内容:

模型/事件

#Data
  attr_accessible :customer_id, :description, :locations_attributes
#Relationship
  has_many :locations
  accepts_nested_attributes_for :locations, :allow_destroy => true
型号/地点

#Data
  attr_accessible :address, :customer_id, :event_id, :latitude, :longitude
#Relationship
  belongs_to :customer
  belongs_to :event
他的观点是这样的

<%= form_for(@event) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <%= f.fields_for :locations do |e| %>
    <%= e.hidden_field :longitude %>
    <%= e.hidden_field :latitude %>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
  def new
    @event = Event.new
    @location = Location.new
    ...
  def create
    @event = current_customer.events.build(params[:event])
    @event.locations.build(params[:locations])
    respond_to do |format|
    ...
问题是,当我创建事件时,会生成表单,它创建事件并创建位置并与事件关联,但我的经度和纬度字段为空。我必须指出它们是浮动类型的,但我不明白为什么它不应该工作

更新:我注意到我可以访问字段,\u,因为我必须将字段\u更改为single

 <%= f.fields_for :location do |e| %>
而params也跟着来了

 "location"=>{"longitude"=>"-80.9460917",
 "latitude"=>"46.4350391"}
这是我的javascript

function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
}
    function showPosition(position)
    {
        document.getElementById('event_location_latitude').value=position.coords.latitude;
        document.getElementById('event_location_longitude').value=position.coords.longitude;
    }
我承认它很难看,但它完成了application.js中的工作,我用表单中的函数调用了它

<script type="text/javascript">
   getLocation();
  </script>

getLocation();
就在表单的前面

请查看此项

<script type="text/javascript">
   getLocation();
  </script>

如果您在查看后有问题。让我知道

在隐藏字段代码中,需要设置一个值。比如说

<%= f.hidden_field :song_id, value: params[:id] %>
<%= f.hidden_field :paid_price,  value: @song.price %>


您是否在属性中放置:位置?cz u显示的错误是无法为您的位置分配质量

我已经检查过了,它与我相同,但不起作用纬度和经度的值确实通过了,我在上面的示例中显示了通过了什么。您正在从value:@song.price获取数据并将其保存到:paid_price。您需要对隐藏字段执行相同的操作。你没有把它指向任何值,这就是为什么它是nilYeah。在页面加载之后,我调用函数getLocations并抓取elementById,然后设置隐藏值的值,但是它是用javascript完成的,我承认它很难看,但是如果我仅仅为了测试而更改为text_字段,我会看到-45.6666这样的值。。。然后将其提交,发送的参数见长度和纬度为隐藏字段。你把它的价值设定在什么地方了吗?如果你从未为经度和纬度设置任何值,那么它将为零。我在代码中没有看到。是的,它们是用javascript添加的,事实上,如果我把它改成文本字段,它们就不会出现。我会更新一些东西,但它存储在哪里?a会议?当你做一个隐藏字段时,它应该是从其他地方带过来的。不,它的html5 getgeolocation,我也记下了,我更新了这个问题,它就像一个圆圈,你需要设置它从哪里提取的值。看看我的例子belowIn事件模型我有一行属性:customer\u id,:description,:locations\u属性,在我的位置我有一行属性:address,:customer\u id,:event\u id,:latitude,:longitude你能试试吗??接受\u嵌套的\u属性\u for:locations\u attributes,:allow\u destroy=>true,这在您的事件模型中获取此错误未找到名称“locations\u attributes”的关联。它已经定义了吗?因为在这个railscast中,它提到,如果您要保护属性为attr\u accessible,那么一定要将嵌套属性添加到其中(例如问题属性和答案属性),所以我猜这可能是你的问题,在我的模型事件中,我有以下位置,即:位置\属性。如果我错过了什么,我不确定