I have user model, and each user has_one
profile model. I want to update profile model. But there just two attributes in User model (first_name, and last_name). So I used accepts_nested_attributes_for
.
When I call update action, I receive the following errors on profile model:
- email can't be blank
- password can't be blank
The following my code:
User Model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
has_one :profile
end
Profile Model:
class Profile < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
end
Profile controller - update action
class ProfilesController < ApplicationController
def profile_params
params.require(:profile).permit(:current_city_id, :current_country_id, :google_plus_page_url, :linkedin_page_url, :facebook_page_url, :skype_id, :user_attributes => [:first_name, :last_name])
end
def update
@profile = Profile.find_by_id(params[:profile_id])
respond_to do |format|
if @profile.update(profile_params)
format.json { render :show, status: :ok, location: @profile }
else
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
end
So, How can I update profile with user's nested attributes without email & password (In Profile Controller Not in devise controller) ??