にdwelling
基づいて構築されたオブジェクトがありますuser
。
Dwellings Controller (作成アクション)
# dwellings_controller.rb
def create
@dwelling = current_user.properties.build(params[:dwelling])
if @dwelling.save
current_user.dwelling = @dwelling
if current_user.save!
flash[:success] = "Woohoo! Your dwelling has been created. Welcome home!"
else
flash[:notice] = "You have successfully created a dwelling, but something prevented us from adding you as a roomie. Please email support so we can try to correct this for you."
end
redirect_to current_user
else
render 'new'
end
end
はレコードdwelling_id
に保存されていません。current_user
詳細情報を取得するために、bang メソッド (上記のとおり) をcurrent_user.save!
コマンドに追加しました。以下に示すように、Rails はuser
、レコードを更新するには のパスワードが必要であると不平を言っています。
ActiveRecord::RecordInvalid in DwellingsController#create
Validation failed: Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
隠しフィールドを介してユーザーのパスワードを提供することが正しい解決策ではなく、安全でないと思われる場合、どうすればこの問題を解決できますか? user
およびdwelling
モデルの関連セクションを以下に示します。
#dwelling.rb
class Dwelling < ActiveRecord::Base
attr_accessible :street_address, :city, :state, :zip, :nickname
belongs_to :owner, :class_name => "User", :foreign_key => "owner_id"
has_many :roomies, :class_name => "User"
#user.rb
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, :password, :password_confirmation, :zip, :dwelling_id
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
belongs_to :dwelling
has_many :properties, :class_name => "Dwelling", :foreign_key => "owner_id"