Accepts_nested_attributes_for 不将子属性保存到数据库

发布于 2024-12-04 17:01:50 字数 2577 浏览 3 评论 0原文

我的 Accepts_nested_attributes_for 仅保存父属性,而不保存子(嵌套)属性。这是父子之间的多对多关系。

问题:没有任何反应,也没有保存任何内容。但在页面上,我收到此消息:

utf8: "\xE2\x9C\x93"
authenticity_token: 9Pjxt7e5RRgWVGafRyMoDqBSqmtj/R2zBSiVxGGxFOI=
parent: !map:ActiveSupport::HashWithIndifferentAccess 
name: "Test"
gender: Male
children_attributes: !map:ActiveSupport::HashWithIndifferentAccess 
"0": !map:ActiveSupport::HashWithIndifferentAccess 
  email: [email protected]
commit: Submit

从终端日志上的消息中,我认为这是因为 Children_attributes 从未保存,因为它被分配了“0”?。这是终端消息:

Started POST "/parents" for 127.0.0.1 at 2011-09-14 11:14:14 -0400
Processing by ParentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9Pjxt7e5RRgWVGafRyMoDqBSqmtj/R2zBSiVxGGxFOI=", "parent"=>{"name"=>"123", "gender"=>"Male", "children_attributes"=>{"0"=>{"email"=>"[email protected]"}}}, "commit"=>"Submit"}
SQL (0.1ms)  BEGIN
SQL (0.6ms)  SELECT 1 FROM `children` WHERE (LOWER(`children`.`email`) = LOWER('[email protected]')) LIMIT 1
SQL (0.2ms)  ROLLBACK

控制器

def new
  @parent = Parent.new
  @parent.children.build
end

def create
  @parent = Parent.new(params[:parent])
  if @parent.save
    redirect_to root_path
  else
    render 'new'
  end
end

- 父模型 -

attr_accessible :children_attributes
has_many :children, :through => :parent_child_relationships

accepts_nested_attributes_for :children

子模型 -

has_many :parents, :through => :parent_child_relationships
validates :email, :name, :presence => true  

父表单视图 -

<%= form_for(@parent, new_parent_path) do |f| %>

<div>
  <%= f.label(:name) %></br>    
  <%= f.text_field(:name) %>
</div>  

<div>
  <%= f.label(:gender) %> <br/>
  <%= f.select(:gender, ['Male', 'Female']) %>
</div>

  <%= f.fields_for :children do |ff| %>

  <div>
    <%= ff.label(:email)%></br>
    <%= ff.text_field(:email)%>
  </div>

  <% end %>

  <%= submit_tag "Submit" %>
<% end %>

任何帮助将不胜感激!谢谢你!

My accepts_nested_attributes_for only saves the parent and not the child (nested) attributes. This is many_to_many relationship between parent and child.

Problem: nothing happens and nothing is saved. But on the page, I get this message:

utf8: "\xE2\x9C\x93"
authenticity_token: 9Pjxt7e5RRgWVGafRyMoDqBSqmtj/R2zBSiVxGGxFOI=
parent: !map:ActiveSupport::HashWithIndifferentAccess 
name: "Test"
gender: Male
children_attributes: !map:ActiveSupport::HashWithIndifferentAccess 
"0": !map:ActiveSupport::HashWithIndifferentAccess 
  email: [email protected]
commit: Submit

From the msg on my terminal log, I think it's because the children_attributes never got saved as it was assigned a "0"?. This is the terminal msg:

Started POST "/parents" for 127.0.0.1 at 2011-09-14 11:14:14 -0400
Processing by ParentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9Pjxt7e5RRgWVGafRyMoDqBSqmtj/R2zBSiVxGGxFOI=", "parent"=>{"name"=>"123", "gender"=>"Male", "children_attributes"=>{"0"=>{"email"=>"[email protected]"}}}, "commit"=>"Submit"}
SQL (0.1ms)  BEGIN
SQL (0.6ms)  SELECT 1 FROM `children` WHERE (LOWER(`children`.`email`) = LOWER('[email protected]')) LIMIT 1
SQL (0.2ms)  ROLLBACK

Controller-

def new
  @parent = Parent.new
  @parent.children.build
end

def create
  @parent = Parent.new(params[:parent])
  if @parent.save
    redirect_to root_path
  else
    render 'new'
  end
end

Parent model -

attr_accessible :children_attributes
has_many :children, :through => :parent_child_relationships

accepts_nested_attributes_for :children

Child model -

has_many :parents, :through => :parent_child_relationships
validates :email, :name, :presence => true  

Parent form view -

<%= form_for(@parent, new_parent_path) do |f| %>

<div>
  <%= f.label(:name) %></br>    
  <%= f.text_field(:name) %>
</div>  

<div>
  <%= f.label(:gender) %> <br/>
  <%= f.select(:gender, ['Male', 'Female']) %>
</div>

  <%= f.fields_for :children do |ff| %>

  <div>
    <%= ff.label(:email)%></br>
    <%= ff.text_field(:email)%>
  </div>

  <% end %>

  <%= submit_tag "Submit" %>
<% end %>

Any help would be greatly appreciated! Thank you!

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

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

发布评论

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

评论(2

表情可笑 2024-12-11 17:01:50

您可以将子模型的验证更改为:

validates :name, :presence => true
validates :email, :presence => true, :email => true

并看看这是否可以解决您的问题?

Can you change your validations on child model to :

validates :name, :presence => true
validates :email, :presence => true, :email => true

And see if that fixes it for you?

就是爱搞怪 2024-12-11 17:01:50

检查子模型是否有任何不成功的验证

您可以使用如下所示的方法在创建时进行验证

  validates_uniqueness_of  :xxxx, :on => :create

Check if there are any unsuccessful validations for the children model

You can validate on create, using something like below

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