Profile model for Devise users?

前端 未结 5 1046
青春惊慌失措
青春惊慌失措 2020-11-28 02:13

I want to extend the sign up form of my devise installation. I created a Profile model and am asking myself now, how can I add specific data of the form to this model. Where

5条回答
  •  悲&欢浪女
    2020-11-28 02:31

    Assuming you have a User model with a has_one Profile association, you simply need to allow nested attributes in User and modify your devise registration view.

    Run the rails generate devise:views command, then modify the devise registrations#new.html.erb view as shown below using the fields_for form helper to have your sign up form update your Profile model along with your User model.

    Sign up

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

    <%= f.label :email %>

    <%= f.text_field :email %>

    <%= f.label :password %>

    <%= f.password_field :password %>

    <%= f.label :password_confirmation %>

    <%= f.password_field :password_confirmation %>

    <%= f.fields_for :profile do |profile_form| %>

    <%= profile_form.label :first_name %>

    <%= profile_form.text_field :first_name %>

    <%= profile_form.label :last_name %>

    <%= profile_form.text_field :last_name %>

    <% end %>

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


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

    And in your User model:

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

提交回复
热议问题