本文介绍了Rails 3.1:accepts_nested_attributes_for 和 has_one 关联 - 行不通?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 has_one 关联模型上使用 accepts_nested_attributes_for,但完全无处可去:-(

I'm trying to use accepts_nested_attributes_for on a has_one association model, and getting absolutely nowhere :-(

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

I have two models, a user and a location. A user has one location:

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

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

I can save changes to the model by using User.find(1).location.current_location_text = "blah" from the console, so I know the associations are set up correctly.

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

I have two forms on the edit user page. One that updates the main user attributes (and works fine and not shown below) and then this one that allows the user to update an attribute of the location model, called "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 %>

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

This doesn't work. I'm slightly confused as the params sent by the form look incorrect. When the form is submitted, this is in the log:

Started PUT "/users/1" for 127.0.0.1 at 2011-10-08 00:28:05 +0100
  Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"YdTAsXwEvRgXIqri+jfx3dLlYG2XWQTuYkgLDsO/OJw=", "location"=>{"current_location_text"=>"E14 8JS"}, "commit"=>"Update Current Location", "id"=>"1"}
  User Load (10.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  User Load (5.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = ? LIMIT 1  [["id", "1"]]
  SQL (4.4ms)  BEGIN
   (2.5ms)  COMMIT
Redirected to http://localhost:3000/users/1

有两件事我觉得很奇怪:

Two things that I find bizarre about this:

  1. 有COMMIT"消息,但没有前面的更新字符串,也没有错误.例如,如果您尝试提交受保护的属性,此时您会收到您无法批量分配..."错误消息.

  1. There's the "COMMIT" message, but with no preceding update string, and no error. eg, if you tried to commit a protected attributed, you'd get the "you can't mass assign..." error message at that point.

参数在我看来是错误的.位置"位按我的预期嵌套,但我也希望它嵌套在用户"哈希中,如下所示:

The params look wrong to me. The "location" bit is nested as I'd expect, but I'd also expect this to be a nested within the "user" hash, something like this:

 {"utf8"=>"✓", "authenticity_token"=>"YdTAsXwEvRgXIqri+jfx3dLlYG2XWQTuYkgLDsO/OJw=", "user"=>{"location"=>{"current_location_text"=>"E14 8JS"}, "commit"=>"Update Current Location", "id"=>"1"}}

我不认为我在这里完全愚蠢.我错过了一些非常明显的东西吗?我试过在我的表单中添加额外的隐藏字段,即用户 ID,然后我得到用户哈希,但与位置"哈希处于同一级别,而不是像我期望的那样作为它的父级!

I don't think I'm being completely stupid here. Am I missing something really obvious? I've tried adding extra hidden fields to my form, ie a user id, and then I get the user hash, but at the same level as the "location" hash, and not as a parent of it as I'd expect!

此外,如果有帮助,这是我在 UsersController 中的更新:

Also if it helps, here's my update within my UsersController:

定义更新@user = User.find(params[:id])

def update @user = User.find(params[:id])

if @user.update_attributes(params[:user])
  redirect_to current_user, :notice => 'User was successfully updated.'
else
  render :action => "edit"
end

结束

这是我的 routes.rb 中的内容(虽然我认为它不相关):

and here's what's in my routes.rb (although I don't think it's relevant):

resources :users do
  resource :location
end

任何帮助表示赞赏.如果我不解决这个问题,笔记本电脑就会跑出窗外......谢谢.

Any help appreciated. If I don't solve this, the laptop is going out the window....Thanks.

推荐答案

<%= fields_for(@user.location) do |location_fields| %>

这是你的问题.您需要在表单中实际嵌套"fields_for,如下所示:

This is your problem. You need to actually "nest" the fields_for inside your form, like this:

<% f.fields_for(@user.location) do |location_fields| -%>

这篇关于Rails 3.1:accepts_nested_attributes_for 和 has_one 关联 - 行不通?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:24