用户:不能大规模指派保护属性 [英] Can't mass-assign protected attributes: user

查看:130
本文介绍了用户:不能大规模指派保护属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个简单的应用程序,要求我提交表单。我创建了两个模型。

I'm working on a simple app that requires me to submit a form. I created two models.

user.rb

class User < ActiveRecord::Base
  attr_accessible :email

  has_many :item
end

item.rb的

class Item < ActiveRecord::Base
  attr_accessible :user_id

  belongs_to :user
end

而不是使用用户表单视图创建一个用户,我试图创建使用项表单视图用户

Instead of creating a user using the user form view, I'm trying to create the user using the item form view.

项/ _form.html.haml

= nested_form_for @item do |form|

  = form.fields_for :user do |builder|
    = builder.text_field :email

  = form.submit "Save"

我错过了一些东西呢?我使用nested_form_for顺便说一句。谢谢你。

Did I miss something here? I'm using nested_form_for btw. Thank you.

推荐答案

找到了答案,我的问题。

Found the answer to my problem.

我更新了我的items_controller.rb

I updated my items_controller.rb

def new
  @item = Item.new
  @item.user = User.new # I added this line.
end

然后,我更新了item.rb的

Then I updated the item.rb

class Item < ActiveRecord::Base
  attr_accessible :user_attributes

  belongs_to :user
  accepts_nested_attributes_for :user
end

这篇关于用户:不能大规模指派保护属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆