私はユーザーモデルと各ユーザーhas_one
プロファイルモデルを持っています。私はプロファイルモデルを更新したい。しかし、Userモデル(first_nameとlast_name)には2つの属性しかありません。だから私はaccepts_nested_attributes_for
を使った。 更新は別のコントローラからネストされた属性としてユーザー属性を作成します
- メールが空白にすることはできません私のコードを
、私は、プロファイルモデルに次のエラーを受け取ります:
ユーザーモデル:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
has_one :profile
end
プロファイルモデル:
class Profile < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
end
プロフィールコントローラ - 更新アクション
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
だから、どのように私は、電子メール&パスワードなしでユーザーのネストされた属性を持つプロファイルを更新することができます(プロファイルコントローラではありませんデマンドコントローラー)?私は更新アクションを呼び出すとき
'validates_format_of:電子メール、と:考案:: email_regexp、allow_blank:true'を –