在不同的页面中嵌入带有额外字段的设计形式

时间:2013-02-03 22:44:24

标签: ruby-on-rails ruby-on-rails-3 devise

我正在使用devise + omniaouth进行身份验证。我的用户也有个人资料,具有以下关系;

class User < ActiveRecord::Base
 has_many :authentications
 has_one :profile
 accepts_nested_attributes_for :profile
 attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes
etc.

所以我的注册/ new.html.erb看起来像这样:

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

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

<h2><%= f.label :email %></h2>
<p><%= f.text_field :email %></p>

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

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

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

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

<%= render "links" %>

正如您所看到的,我尝试从配置文件中填充的唯一属性是“名称”。这可以正常工作,当您尝试从/ users / sign_up注册时,您必须填写姓名,电子邮件和密码。

现在,我正在尝试从模态窗口显示此注册窗口,因此我创建了: /views/devise/menu/_registration_items.html.erb具有相同的信息:

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

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

<h2><%= f.label :email %></h2>
<p><%= f.text_field :email %></p>

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

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

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

<br/>

<% end %>

我还将它添加到应用程序控制器(如果我把它放在帮助程序中,它的工作方式相同):

 def resource_name
   :user
 end

 def resource
   @resource ||= User.new
 end

 def devise_mapping
   @devise_mapping ||= Devise.mappings[:user]
 end

https://github.com/plataformatec/devise/wiki/How-To%3a-Display-a-custom-sign_in-form-anywhere-in-your-app

所述

除了名称字段不会在表单中显示之外,一切正常。(例如,没有该字段名称的登录表单可以正常工作)。我想我必须映射别的东西,但我不知道它是什么。

谢谢!

1 个答案:

答案 0 :(得分:1)

我忘了添加

<% resource.build_profile %>

位于表单顶部。