无法批量分配受保护的属性:user
我正在开发一个简单的应用程序,要求我提交表单。 我创建了两个模型。
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
我没有使用用户表单视图创建用户,而是尝试使用项目表单视图创建用户。
项目/ _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 btw。 谢谢。
试试这个
attr_accessible :email :user
试试这段代码
class Item < ActiveRecord::Base attr_accessible :user_id accepts_nested_attributes_for :user # <--- This should help belongs_to :user end class User < ActiveRecord::Base attr_accessible :email has_many :items # <--- Typofix end
找到了我的问题的答案。
我更新了我的items_controller.rb
def new @item = Item.new @item.user = User.new # I added this line. end
然后我更新了item.rb
class Item < ActiveRecord::Base attr_accessible :user_attributes belongs_to :user accepts_nested_attributes_for :user end
- 为什么accept_nested_attributes_for allow_destroy不起作用?
- XML序列化在Rails模型的datetime字段中不包括毫秒
- 如何通过尚未保存的父关联访问ActiveRecord祖父母关联?
- 在Rails has_many中保存关联记录的顺序:通过关联
- ActiveRecord where.not不工作/奇怪的行为
- 如何在Ruby on Rails中获取ActiveRecord执行的最后一个SQL查询?
- 如何最好地处理与ActiveRecord的每个模型数据库连接?
- Ruby on Rails:如何在没有Active Record的情况下validation模型?
- 奇怪的Ruby on Rails错误