小编Dav*_*ens的帖子

Rails 3.1:accepts_nested_attributes_for和has_one关联 - 不起作用?

我正在尝试在has_one关联模型上使用accepts_nested_attributes_for,并且绝对无处:-(

我有两个模型,一个用户和一个位置.用户有一个位置:

class User < ActiveRecord::Base
  # current location
  has_one :location, :dependent => :destroy
  accepts_nested_attributes_for :location
end

class Location < ActiveRecord::Base
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

我可以通过User.find(1).location.current_location_text = "blah"控制台使用来保存对模型的更改,因此我知道关联设置正确.

我在编辑用户页面上有两个表单.一个更新主要用户属性(并且工作正常并且未在下面显示)然后允许用户更新位置模型的属性,称为"current_location_text":

<%= form_for(@user) do |f| %>  
    <%= fields_for(@user.location) do |location_fields| %>
        <%= location_fields.label :current_location_text, 'Current Location' %>
        <%= location_fields.text_field :current_location_text, :placeholder => 'Road, City or Postcode' %>
    <% end %>

    <%= f.submit "Update Current Location" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

这不起作用.我有点困惑,因为表格发送的参数看起来不正确.提交表单时,这是在日志中:

Started PUT "/users/1" for 127.0.0.1 at 2011-10-08 00:28:05 +0100
  Processing by …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-3.1

8
推荐指数
2
解决办法
7323
查看次数

标签 统计

ruby-on-rails ×1

ruby-on-rails-3.1 ×1