Nested attributes and belongs_to association - ruby-on-rails

Rails 4.2.8
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :rememberable,
:validatable, :encryptable, :omniauthable, omniauth_providers: [:facebook], encryptor: :restful_authentication_sha1
attr_accessible :email, :name, :password, :password_confirmation, :is_admin, :is_master
has_one :customer
accepts_nested_attributes_for :customer
end
customer.rb
class Customer < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
end
customers_contoller.rb
class CustomersController < ApplicationController
def edit
#customer = current_user.customer
end
def update
#customer = current_user.customer
#customer.update customer_params
render 'edit'
end
private
def customer_params
params.require(:customer).permit(:first_name, :last_name, :phone, user_attributes: [:email, :password, :password_confirmation])
end
end
customers/edit.html.erb
<%= form_for #customer, html: { class: 'checkout-attributes profile-form' } do |f| %>
<div class="row">
<div class="col-sm-6">
<span class="help-block">First Name</span>
<%= f.text_field :first_name, class: 'form-control' %>
</div>
<div class="col-sm-6">
<span class="help-block">Last Name</span>
<%= f.text_field :last_name, class: 'form-control' %>
</div>
<div class="col-sm-12">
<span class="help-block">Email</span>
<%= fields_for :user, #customer.user do |u| %>
<%= u.email_field :email, class: 'form-control' %>
<% end %>
</div>
<div class="col-sm-12">
<span class="help-block">Phone Number</span>
<%= f.text_field :phone, class: 'form-control' %>
</div>
<div class="col-sm-12">
<span class="help-block">Password</span>
<%= fields_for :user, #customer.user do |u| %>
<%= u.password_field :password, class: 'form-control' %>
<% end %>
</div>
<div class="col-sm-12">
<span class="help-block">Confirm Password</span>
<%= fields_for :user, #customer.user do |u| %>
<%= u.password_field :password_confirmation, class: 'form-control' %>
<% end %>
</div>
</div>
<% end %>
That's what I see in log
Started PATCH "/customers/560738" for 127.0.0.1 at 2015-11-04 08:15:20-0500 Processing by CustomersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OL7NCIMKCb+LQZ+1voahUsNyD37q6sal8W7HsV4EKKP9ABMuDVGqxs0nFS2Cyo7A6XBkRLYv9tjTYH4kaHNdkA==", "image"=>"", "customer"=>{"first_name"=>"Jack", "last_name"=>"Drobazko", "phone"=>"5084427293"}, "user"=>{"email"=>"ddd#fff.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "id"=>"560738"}
Also I see that customer_params methods gives filtered (without user part) hash {"first_name"=>"Jack", "last_name"=>"Drobazko", "phone"=>"5084427293"}
So how to make it works?

According to your log file:
"image"=>"", "customer"=>{"first_name"=>"Jack",
"last_name"=>"Drobazko", "phone"=>"5084427293"},
"user"=>{"email"=>"ddd#fff.com", "password"=>"[FILTERED]",
"password_confirmation"=>" ...
All your fields_for inputs outside of a main form. Fix your form to wrap all user input in the fields_for helper.
Note about f.fields_for and simple fields_for, it's very important
<%= form_for #customer, html: { class: 'checkout-attributes profile-form' } do |f| %>
# some code here
<%= f.fields_for :user, #customer.user do |u| %>
<%= u.email_field :email, class: 'form-control' %>
<%= u.password_field :password, class: 'form-control' %>
<%= u.password_field :password_confirmation, class: 'form-control' %>
<% end %>
</div>
# some code here
Here is wonderful article from Rails core team, about using nested attributes.

Related

Rails Devise: "Foreign Key was set to nil" error when Updating nested Attributes on Form

I am creating a form to update a User in my Rails app with Devise.
I have separated my User data into a User model for the email and password, and a profile table for all the other data.
Created a form to update these details. Form renders fine, and parameters are being sent, but I am getting this error when I try to update the first_name for my nested record.
ActiveRecord::RecordNotSaved in Devise::RegistrationsController#update
Failed to remove the existing associated profile. The record failed to save after its foreign key was set to nil.
Extracted source (around line #93):
if target.persisted? && owner.persisted? && !target.save
set_owner_attributes(target)
raise RecordNotSaved, "Failed to remove the existing associated #{reflection.name}. "
"The record failed to save after its foreign key was set to nil."
end
Models look like
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :profile
after_create :create_profile
accepts_nested_attributes_for :profile
end
class Profile < ApplicationRecord
belongs_to :user
end
Controllers look like
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u|
u.permit(:email, :password, [profile_attributes: [:id, :first_name, :last_name]])
}
devise_parameter_sanitizer.permit(:account_update) { |u|
u.permit(:email, :password, [profile_attributes: [:id, :first_name, :last_name]])
}
end
end
class UsersController < Devise::RegistrationsController
def create
super
end
def show
#user = current_user
end
def edit
#user = current_user
super
end
def update
#user = current_user
super
end
end
and the View looks like
<h1>Account Details</h1>
<p><strong>Email Address:</strong> <%= #user.email %></p>
<p><strong>First Name:</strong> <%= #user.profile.first_name %></p>
<p><strong>Last Name:</strong> <%= #user.profile.last_name %></p>
<p><strong>Description:</strong> <%= #user.profile.description %></p>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<%= f.fields_for :profile_attributes, {html: { method: :put}} do |p| %>
<div class="field">
<%= p.label :first_name %><br />
<%= p.text_field :first_name %>
</div>
<% end %>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "new-password" %>
<% if #minimum_password_length %>
<br />
<em><%= #minimum_password_length %> characters minimum</em>
<% end %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "current-password" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<br><br>
<%= button_to "Delete Account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
How do I resolve this?
You need to use existing user's profile inside the form. Change line with fields_for to:
<%= f.fields_for resource.profile do |p| %>
Note, you don't need a method here, since it is not a separate form

Cannot get a nested model to save in rails 5

I have a user which is a nested resource of account, and I'm trying to create the account and the user in 1 form. The account is saving correctly, however no user record is being written.
user.rb:
class User < ApplicationRecord
belongs_to :account
validates :email, presence: true
end
account.rb:
class Account < ApplicationRecord
has_many :users, :dependent => :destroy
accepts_nested_attributes_for :users
end
accounts_controller.rb:
class AccountsController < ApplicationController
def new
#account = Account.new
#user = #account.users.build
end
def create
#account = Account.new(account_params)
#account.secret = SecureRandom.uuid
if #account.save
flash[:success] = "You've successfully created your account, now it's time to create your first team."
redirect_to dashboard_path
else
flash[:danger] = "There was a problem creating your account"
redirect_to signup_path
end
end
private
def account_params
params.require(:account).permit(:name, user_attributes: [:first_name, :last_name, :email, :password, :password_confirmation, :secret])
end
end
accounts/new.html.erb:
<h1>Signup</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#account) do |form| %>
<%= form.label "Account name" %>
<%= form.text_field :name, class: "form-control" %>
<%= form.fields_for :user do | f| %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: "form-control" %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: "form-control" %>
<%= f.label :email %>
<%= f.text_field :email, class: "form-control" %>
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: "form-control" %>
<% end %>
<%= form.submit "Signup", class: "btn btn-primary" %>
<% end %>
</div>
</div>
When I submit the the form and check my logs, I'm seeing:
Processing by AccountsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"p5+CRICRp3ZilIg1NtehEQG7Gh2amnFFUD5NawtqkICJ4uRDvvJxf2WTbd7+rnfG9zdblT1QWgfJ62NNxcc2RA==", "account"=>{"name"=>"Streame", "user"=>{"first_name"=>"Jeremy", "last_name"=>"Kirkham", "email"=>"jeremy#streame.com.au", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}, "commit"=>"Signup"}
Unpermitted parameter: user
The answer was quite simple in the end - I needed to change the form from
<%= form.fields_for :user do | f| %>
to
<%= form.fields_for :users do | f| %>
(note user -> users), and the controller from
params.require(:account).permit(:name, user_attributes:
to
params.require(:account).permit(:name, users_attributes:
(again note user -> users).
Basically it came down to pluralisation!

How to create a profile for devise user?

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.
Model - 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
Model - user_profile.rb
class UserProfile < ActiveRecord::Base
belongs_to :user
end
Controller - 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
View - 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" %>
Server Log
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 ?
please follow the below steps.
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
Have you tried following these steps from their github page?
They set the permitted parameters in the ApplicationController. Also the structure is a little different. The parameters go into keys.
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
added_attrs = [:username, :email, :password, :password_confirmation, :remember_me]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end
end

Nested Form for Join Table

I'm attempting to add a nested form into the Devise registration form.
My 3 tables are Users (devise), Languages, and they both are joined in Languages_Users
It signs up the new user, but does not save Languages_Users, but I see that it is passed in the params. A new issue has also arisen - the avatar file is no longer saved. I'm not sure if these are related (if not, don't worry about addressing it).
**UPDATE - Here are my current logs - I now think the issue due to languages_users not receiving user_id
"languages users can't be blank'. In my logs though, it says 'SELECT 1 AS one FROM "languages_users" WHERE ("languages_users"."user_id" IS NULL AND "languages_users"."language_id" = 2) LIMIT 1' Is this an issue with not passing the user_id?
**
User.rb
class User < ActiveRecord::Base
has_many :languages_users
has_many :languages, :through => :languages_users
accepts_nested_attributes_for :languages_users
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :role
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
validates_presence_of :first_name, :last_name, :location, :nationality, :bio
end
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
def new
build_resource({})
resource.languages_users.build
respond_with self.resource
end
def create
super
end
private
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:email, :password, :password_confirmation, :bio, :location, :last_name, :first_name, :nationality, :avatar, languages_user_attributes: [:id, :language_id, :user_id, :level]) }
end
end
in registrations:
new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, as: resource, url: registration_path(resource)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :nationality %><br/>
<%= f.text_field :nationality %>
</div>
<div class="field">
<%= f.label :bio %><br />
<%= f.text_field :bio %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="form-group">
<%= f.label :avatar, class: 'col-sm-2 control-label' %>
<div class="col-sm-6">
<%= f.file_field "user[avatar]" %>
</div>
</div>
<%= f.fields_for :langauges_user do |lu| %>
<br>
<div class="fields">
<%= lu.collection_select(:language_id, Language.order('language ASC').all, :id, :language) %><br>
<%= lu.hidden_field :level, value: 1 %>
<% end %>
</div>
<br>
<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>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
logs:
Started POST "/users" for ::1 at 2016-03-26 13:30:40 -0400
Processing by Users::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x1F0kXqKN2/uu7S6BLxyBgSatcVWiSOLASEYYJ7ZF0b3d8V8O+FVQAO0yhjTJ2LImI+Xy4j7Rn+SvlYjV07mrA==", "user"=>{"first_name"=>"John", "last_name"=>"Smith", "location"=>"NYC", "nationality"=>"American", "bio"=>"hello", "email"=>"johns#gmail.com", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f940b16a4f8 #tempfile=#<Tempfile:/var/folders/cn/l75pvjk9707bj93z_yykb0t40000gn/T/RackMultipart20160326-11607-1fg5ncg.JPG>, #original_filename="IMG_4573.JPG", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"user[user[avatar]]\"; filename=\"IMG_4573.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "langauges_users"=>{"language_id"=>"16", "level"=>"1"}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameters: user, langauges_users
Still a newb, so please let me know if you'd like me to provide anything further.
Thanks!
You should change languages_user to languages_users
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:email, :password, :password_confirmation, :bio, :location, :last_name, :first_name, :nationality, :avatar, languages_users_attributes: [:id, :language_id, :user_id, :level]) }
end
Also in fields_for
<%= f.fields_for :languages_users do |lu| %>

Rails 4 and Devise: Having on field value (email) in the registration form, entered into both parent and nested model tables at sign up

I am using devise.
I have a member model (which is the model that devise uses)
A member has a 1 to 1 relationship with a user.
I have a user model that contains all the profile information of the user.
The purpose of this is to keep the users info separate from devise.
I have a sign up form that has a nested form for user...therefore when the member signs up, on that same page they enter their info and that gets put in the user table.
The issue is simple enough.
I have one email field on the form (for member).
I want the value of email to also get entered into the database on save.
I tried
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_one :user, dependent: :destroy, autosave: true
accepts_nested_attributes_for :user
before_create :create_user
def create_user
user = User.new(:email => self.email)
user.save
end
end
But what is actually happening with this is that 2 users get saved into the user table. One with the email field filled in and the rest blank, and a second with all the other attributes filled in but no email.
Here is my custom registration controller
class Members::RegistrationsController < Devise::RegistrationsController
# GET /resource/sign_up
def new
build_resource({})
resource.build_user
respond_with self.resource
end
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:email, :password, :password_confirmation, :user_attributes => :first_name)
}
end
private
def sign_up_params
params.require(:member).permit(:email, :password, :password_confirmation, user_attributes: [:member_id, :email, :first_name, :last_name, :institution, :job_title, :about, :picture])
end
end
The question is how can I get the email value into the member table AND the user table (along with the other data)
Here is the form. Don't like posting all this, but I guess it maybe required.
<div class="col-md-8 col-md-offset-2 col-sm-8 col-sm-offset-2 col-xs-12">
<%= bootstrap_devise_error_messages! %>
<div class="panel panel-default">
<div class="panel-heading">
<h4><%= t('.sign_up', :default => "Sign up") %></h4>
</div>
<div class="panel-body">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { role: "form" }) do |f| %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %><br />
<%= f.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: "form-control" %>
</div>
<%= f.fields_for(:user) do |user_fields| %>
<div class="form-group">
<%= user_fields.label :first_name %><br>
<%= user_fields.text_field :first_name, autofocus: true, class: 'form-control' %>
</div>
<div class="form-group">
<%= user_fields.label :last_name %><br>
<%= user_fields.text_field :last_name, class: 'form-control' %>
</div>
<div class="form-group">
<%= user_fields.label :job_title %><br>
<%= user_fields.text_field :job_title, class: 'form-control' %>
</div>
<div class="form-group">
<%= user_fields.label :institution %><br>
<%= user_fields.text_field :institution, class: 'form-control' %>
</div>
<div class="form-group">
<%= user_fields.label :about %><br>
<%= user_fields.text_area :about, class: 'form-control' %>
</div>
<span class="picture">
<%= user_fields.label :picture, "Upload photo" %>
<%= user_fields.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<% end %>
<%= f.submit t('.sign_up', :default => "Sign up"), class: "btn btn-primary" %>
<% end %>
</div>
</div>
<%= render "devise/shared/links" %>
</div>
def create_user
user = User.new(:email => self.email)
user.save
end
This is doing exactly what you are asking it to do, which is create a new User object.
The easiest way to accomplish what you want, is to update the associated User object once your Member object is saved. Like this
# member.rb
after_save :update_user
def update_user
user.email = email
user.save
end
Here, you are not creating a new User object, you are referencing the User object that is associated to the Member object via that has_one association you specified earlier.

Resources