如何为设计用户创建配置文件? [英] How to create a profile for devise user?

查看:81
本文介绍了如何为设计用户创建配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的新手,它试图在devise用户注册时使用devise注册中的嵌套表单为他们创建一个配置文件。我已经完成了
为Devise用户创建个人资料
Devise用户的个人资料模型?
和其他几篇文章来实现一样,但是白费了一天之后,我仍在努力使它起作用。这是我的代码。

I'm new to rails and trying to create a profile for devise users when they signup, using nested form in devise signup. I've gone through Creating Profile for Devise users, Profile model for Devise users? and few other articles to achieve the same but after a day in vain, I'm still trying to make it work. Here is my code.

模型-user.rb

class User < ActiveRecord::Base
  has_one :user_profile
  accepts_nested_attributes_for :user_profile

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

模型-user_profile.rb

class UserProfile < ActiveRecord::Base
  belongs_to :user
end

控制器- controllers / users / registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) {|u| 
      u.permit(:email, :password, :password_confirmation, :remember_me, 
      user_profile_attributes: [:first_name, :last_name])}
    end
  end
end

视图-views / devise / registrations / new.html.erb

 <h2>Sign up</h2>
 <% resource.build_user_profile %>
 <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
   <%= devise_error_messages! %>

   <div class="field">
     <%= f.label :email %><br />
     <%= f.email_field :email, autofocus: true %>
   </div>

   <div class="field">
     <%= f.label :password %>
     <% if @minimum_password_length %>
       <em>(<%= @minimum_password_length %> characters minimum)</em>
     <% end %><br />
     <%= f.password_field :password, autocomplete: "off" %>
   </div>

   <div class="field">
     <%= f.label :password_confirmation %><br />
     <%= f.password_field :password_confirmation, autocomplete: "off" %>
   </div>

   <%= f.fields_for :user_profile do |profile_form| %>
     <%= profile_form.label :first_name %><br/>
     <%= profile_form.text_field :first_name %><br/>

     <%= profile_form.label :last_name %><br/>
     <p><%= profile_form.text_field :last_name %><br/>
   <% end %>

   <div class="actions">
     <%= f.submit "Sign up" %>
   </div>
 <% end %>

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

服务器日志

Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"√", "authenticity_token"=>"rLuFXwISxiJpWPjpmKzjnjhKr41F5
56sWbtT+8gslAMsFDWRbl7MSitSXUESjLdZccCBGBGvVv+JbhW7G5py5g==", "user"=>{"email"=>
"zebandz@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTER
ED]", "user_profile_attributes"=>{"first_name"=>"Zeban", "last_name"=>"Dezend"}}
, "commit"=>"Sign up"}
Unpermitted parameter: user_profile_attributes

我想,我缺少从参数中获取值并创建新记录的代码。有人可以向我建议解决方法吗?

I think, I'm missing the code to fetch the values from params and create a new record. Can someone suggest me the fix ?

推荐答案

请按照以下步骤操作。

devise> reg> new

devise> reg > new

    <div class="row">
          <div class="col-md-5 col-md-offset-4">

          <h2>Sign up</h2>

          <% resource.build_user_profile if resource.user_profile.nil? %>
          <%= form_for(resource, as: resource_name, url:     registration_path(resource_name)) do |f| %>
          <%= devise_error_messages! %>

           <div class="row">
           <%= f.fields_for :user_profile do |profile_form| %>
           <div  class="col-md-6">
           <div class="form-group">
             <%= profile_form.label :first_name %>
             <%= profile_form.text_field :first_name, class: "form-control" %>
           </div>
           </div>
           <div  class="col-md-6">
           <div class="form-group">
             <%= profile_form.label :last_name %>
             <%= profile_form.text_field :last_name, class: "form-control" %>
          </div>
          </div>
          <% end %>
          </div>

          <div class="form-group">
          <%= f.label :email %><br />
          <%= f.email_field :email, autofocus: true, class: "form-control" %>
</div>

          <%= f.fields_for :user_profile do |profile_form| %>
          <div class="form-group">
           <%= profile_form.label :business_name %>
           <%= profile_form.text_field :business_name, class: "form-control" %>
          </div>
          <div class="form-group">
           <%= profile_form.label :business_category %>
           <%= profile_form.collection_select :business_category_id, BusinessCategory.all, :id, :name, {prompt: "Select Category"}, {class: "form-control"} %>
          </div>
          <div class="form-group">
            <%= profile_form.label :website %>
            <%= profile_form.url_field :website, class: "form-control" %>
          </div>
         <div class="form-group">
           <%= profile_form.label :address %>
           <%= profile_form.text_area :address, class: "form-control" %>
         </div>
         <div class="form-group">
            <%= profile_form.label :personal_number %>
            <%= profile_form.text_field :phone_number, class: "form-control" %>
         </div>


    <div class="form-group">
         <%= profile_form.label :office_number %>
         <%= profile_form.text_field :office_number, class: "form-control" %>

   </div>
<% end %> 

<div class="form-group">
  <%= f.label :password %>
  <% if @minimum_password_length %>
  <em>(<%= @minimum_password_length %> characters minimum)</em>
  <% end %><br />
  <%= f.password_field :password, autocomplete: "off", class: "form-control" %>
</div>

<div class="form-group">
  <%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
</div>

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

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



user reg controller

def new
    # Override Devise default behaviour and create a profile as well
    build_resource({})
    resource.build_user_profile
    respond_with self.resource
  end

protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) { |u|
      u.permit(:email, :password, :password_confirmation, :user_profile_attributes => [:first_name, :last_name, :business_name, :business_category_id, :website, :address, :phone_number, :office_number])
    }
  end

这篇关于如何为设计用户创建配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆