gpt4 book ai didi

devise - Rails 3 fields_for has_one 不使用设备渲染表单字段

转载 作者:行者123 更新时间:2023-12-02 09:35:33 24 4
gpt4 key购买 nike

您好,我使用 devise 来注册用户,我想在每次用户注册时创建一个与用户相关的配置文件,问题是当我尝试在配置文件模型中添加该人的全名时设备用户注册的注册 View ,它不显示...

这是我的模型:

class User < ActiveRecord::Base

# Include default devise modules. Others available are:
# :token_authenticatable, :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, dependent: :destroy
accepts_nested_attributes_for :profile
# attr_accessible :title, :body
end

这是配置文件模型

class Profile < ActiveRecord::Base
attr_accessible :email, :name, :phone, :code
belongs_to :user

validates_presence_of :user
end

这是修改后的设备 View :

<% provide(:title, 'Sign up' ) %>
<h2>Sign up</h2>

<div class="row">
<div class="span6 offset3">

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= render 'shared/error_messages', object: resource %>

<%= f.fields_for :profile do |profile_form| %>
<%= profile_form.label :full_name %>
<%= profile_form.text_field :name %>
<% end %>

<%= f.label :email %>
<%= f.email_field :email %>

<%= f.label :password %>
<%= f.password_field :password %>

<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>

<div class="form-actions">
<%= f.submit "Sign up", class: "btn btn-large btn-primary" %>
</div>
<% end %>

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

</div>
</div>

我看不出为什么名称文本字段不会出现......

感谢您的帮助

最佳答案

弄清楚了,它太简单了,我感觉很糟糕......所以这里是:

我忘记在表单上生成新的配置文件,所以它最终看起来像这样:

.
.
.

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= render 'shared/error_messages', object: resource %>

<div class="invisible">
<%= resource.build_profile %>
</div>

<%= f.fields_for :profile do |profile_form| %>
<%= profile_form.label :full_name %>
<%= profile_form.text_field :name %>

<% end %>

<%= f.label :email %>
<%= f.email_field :email %>

.
.
.

希望这也能解决其他人的问题!

谢谢!

关于devise - Rails 3 fields_for has_one 不使用设备渲染表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12335865/

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