Swa*_*y g 23 ruby ruby-on-rails nested-forms
我有一个用户和个人资料模型.一个用户可以拥有许多配置文件 在用户创建过程中,我只需要在我的用户模型中访问配置文件部分中的一个信息(即电话号码).因此,我正试图通过它完成它attr_accessible.我的user.rb看起来像这样.
has_many :profiles
attr_accessible :handle, :email, :password, :profile_mobile_number
attr_accessor : :profile_mobile_number
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,当我尝试在user.rb中的方法中调用getter方法profile_mobile_number时(该方法是私有的,虽然我认为无所谓),但我得到一个空值.我在users/new.html.erb表单中使用以下内容
我的问题是这样做的正确方法是什么?我应该使用<% f.fields_for :profile do |ff| -%>或<% f.fields_for :profiles do |ff| -%>(注意第二个是复数).当我使用复数:配置文件时,我甚至看不到表单上的字段.我在这里错过了什么?什么是需要在模型user.rb中使用的时态?:profile_phone_number或:profiles_phone_number?谢谢.
Pra*_*ran 20
您可以执行以下操作:
<% form_for @user, :url => { :action => "update" } do |user_form| %>
...
<% user_form.fields_for :profiles do |profiles_fields| %>
Phone Number: <%= profiles_fields.text_field :profile_mobile_number %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
但既然你已经有了一个关联,那么不妨使用' accepts_nested_attributes_for '
| 归档时间: |
|
| 查看次数: |
31435 次 |
| 最近记录: |