Reputation: 3333
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"=>"[email protected]", "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?
Upvotes: 0
Views: 445
Reputation: 44380
According to your log file:
"image"=>"", "customer"=>{"first_name"=>"Jack", "last_name"=>"Drobazko", "phone"=>"5084427293"}, "user"=>{"email"=>"[email protected]", "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
.
Upvotes: 1