【问题标题】:Embed devise form with extra field in different page在不同页面中嵌入带有额外字段的设计表单
【发布时间】:2013-02-03 22:44:24
【问题描述】:

我正在使用 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所解释的那样

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

    忘记加了

    <% resource.build_profile %>
    

    表格顶部的一行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 2011-06-08
      • 2012-07-19
      • 1970-01-01
      相关资源
      最近更新 更多