如何让nested_attributes在Rails3中工作?

发布于 2024-10-09 05:39:23 字数 1290 浏览 3 评论 0原文

这是我的用户模型:

class User < ActiveRecord::Base
  has_one :teacher, :dependent => :destroy
  accepts_nested_attributes_for :teacher, :allow_destroy => true
  attr_accessible :email, :password, :password_confirmation, :remember_me, :teacher_attributes
end

这是我的教师模型:

class Teacher < ActiveRecord::Base
  belongs_to :user
  attr_accessible :user_id, :first_name, :last_name
  validates_presence_of :user_id, :first_name, :last_name
end

这是我的形式:

<%= form_for(@user, :url => registration_path(:user)) do |user| %>


        <%= user.text_field :email %>
        <%= user.text_field :password %>
        <%= user.text_field :password_confirmation %>


    <%= user.fields_for resource.build_teacher do |t| %>
            <%= t.text_field :first_name %>
            <%= t.text_field :last_name %>
            <%= t.text_field :phone %>
    <% end %>

      <%= user.submit 'Confirm' %>
<% end %>

除了这个东西不会“接受嵌套属性” 我的开发日志说:

WARNING: Can't mass-assign protected attributes: teacher

我不知道它是否相关,但表单没有在 Teacher_attributes 数组或任何内容中生成字段 - 它在教师内部。我猜这就是我的问题所在,但我不知道如何将字段放入其中。请帮忙。

谢谢!

Here's my user model:

class User < ActiveRecord::Base
  has_one :teacher, :dependent => :destroy
  accepts_nested_attributes_for :teacher, :allow_destroy => true
  attr_accessible :email, :password, :password_confirmation, :remember_me, :teacher_attributes
end

Here's my teacher model:

class Teacher < ActiveRecord::Base
  belongs_to :user
  attr_accessible :user_id, :first_name, :last_name
  validates_presence_of :user_id, :first_name, :last_name
end

Here's my form:

<%= form_for(@user, :url => registration_path(:user)) do |user| %>


        <%= user.text_field :email %>
        <%= user.text_field :password %>
        <%= user.text_field :password_confirmation %>


    <%= user.fields_for resource.build_teacher do |t| %>
            <%= t.text_field :first_name %>
            <%= t.text_field :last_name %>
            <%= t.text_field :phone %>
    <% end %>

      <%= user.submit 'Confirm' %>
<% end %>

Except that this thing won't "accept nested attributes"
My development log says:

WARNING: Can't mass-assign protected attributes: teacher

I don't know if it's related, but the form isn't generating fields inside a teacher_attributes array or anything - it's inside teacher. I'm guessing that's where my problem is, but I don't how to make it put the fields inside it. Please help.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2024-10-16 05:39:23

尝试以下操作:

在视图顶部:

<% @user.build_teacher if @user.teacher.nil? %>

对于字段:

<%= user.fields_for :teacher do |t| %>

另外,就我个人而言,我喜欢在表单中命名块参数(|user||t| 部分) 作为 |form| (因为当您度过了漫长的一天,并且您在视图中看到 user 而不是 form 时,它可能会让你感到困惑!)

Try these things:

At top of view:

<% @user.build_teacher if @user.teacher.nil? %>

For the fields for:

<%= user.fields_for :teacher do |t| %>

Also, personally, I like naming the block parameters in forms (the part |user| and |t|) as |form| (because when you're having a long day, and you see user down in the view and not form, it can confuse you!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文