gpt4 book ai didi

ruby-on-rails - Rails 3 嵌套表单 - 多级

转载 作者:行者123 更新时间:2023-12-04 05:50:52 26 4
gpt4 key购买 nike

我在我的应用程序中定义了以下模型

  • 用户
  • 简介
  • 地址

这是我的用户模型:

class User < ActiveRecord::Base

# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes

has_one :profile
accepts_nested_attributes_for :profile
end

这是我的个人资料模型:

class Profile < ActiveRecord::Base

attr_accessible :first_name, :last_name, :organization, :telephone_number, :user_id, :address_attributes

belongs_to :user
has_one :address

accepts_nested_attributes_for :address
end

这是我的地址模型:

class Address < ActiveRecord::Base
attr_accessible :street, :street_cont, :city, :state, :zip_code
belongs_to :profile
end

我正在使用设计进行身份验证,所以在我看来我有以下注册:

<% resource.build_profile %>
<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<p><%= f.label :email %><br />
<%= f.email_field :email %></p>

<p><%= f.label :password %><br />
<%= f.password_field :password %></p>

<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>

<%=f.fields_for :profile do |profile_form| %>
<p><%= profile_form.label :first_name %><br />
<%= profile_form.text_field :first_name %></p>

<p><%= profile_form.label :last_name %><br />
<%= profile_form.text_field :last_name %></p>

<p><%= profile_form.label :organization %><br />
<%= profile_form.text_field :organization %></p>

<p><%= profile_form.label :telephone_number %><br />
<%= profile_form.text_field :telephone_number %></p>

<%=f.fields_for :address do |address_form| %>
<p><%= address_form.label :street %><br />
<%= address_form.text_field :street %></p>

<p><%= address_form.label :street_cont %><br />
<%= address_form.text_field :street_cont %></p>

<p><%= address_form.label :city %><br />
<%= address_form.text_field :city %></p>

<p><%= address_form.label :state %><br />
<%= address_form.text_field :state %></p>

<p><%= address_form.label :zip_code %><br />
<%= address_form.text_field :zip_code %></p>
<% end %>
<% end %>

<p><%= f.submit "Sign up" %></p>
<% end %>

<%= render :partial => "devise/shared/links" %>

表单正确呈现,但是当我查看源代码时,我看到地址字段是这样的:

<input id="user_address_street" name="user[address][street]" size="30" type="text" />

我看到的个人资料部分:

<input id="user_profile_attributes_first_name" name="user[profile_attributes][first_name]" size="30" type="text" />

当我保存表单时,用户和个人资料会保存到数据库中,但不会保存地址。显然,我的模型关系可能做错了,但我不知道如何解决这个问题。

如有任何帮助,我们将不胜感激。

最佳答案

改变这个:

 <%=f.fields_for :address do |address_form| %>

对此:

<%=profile_form.fields_for :address do |address_form| %>

关于ruby-on-rails - Rails 3 嵌套表单 - 多级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5969429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com