1

我正在使用 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.

所以我的 registrations/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所述

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

谢谢!

4

1 回答 1

1

我忘了添加

<% resource.build_profile %>

表格顶部的行。

于 2013-02-05T00:43:06.377 回答