0

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"
4

2 に答える 2

1

問題は、Userモデルでの検証に起因していたことが判明しました。およびのpresenceおよびのlength検証が に課されていましたが、およびが提供されていませんでした。私の更新の検証は以下に表示されます。passwordpassword_confirmationuser.savepasswordpassword_confirmation

ユーザーモデル

#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_create :create_remember_token

  belongs_to :dwelling
  has_many :properties, :class_name => "Dwelling", :foreign_key => "owner_id"

  validates :first_name, presence: true, length: { maximum: 50 }
  validates :last_name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, 
                format: { with: VALID_EMAIL_REGEX },
                uniqueness: { case_sensitive: false }
  validates :password, presence: { :on => :create }, length: { minimum: 6, :on => :create }
  validates :password_confirmation, presence: { :on => :create }

この更新された実装では、userが正常に保存され、適切な属性が更新されます。これは、この元の質問に起因する二次的な問題です: Rails では、新しいオブジェクトの Create メソッド内で別のクラスの新しく作成されたオブジェクトを所有するようにユーザーを自動的に更新するにはどうすればよいですか?

于 2012-08-05T05:40:33.360 に答える
0

ユーザーのパスワードは (has_secure_password を介して) 暗号化されたパスワード ダイジェスト形式でデータベースに保存され、復号化できないため、フォームのどこにもパスワードを割り当てることはできません。(パスワードは一方向暗号化で保存されます。) したがって、フォームの非表示フィールドのどこかに入力するためにパスワードを取得する方法はありません (とにかく悪い考えです)。

ログ (logger.debug current_user) で current_user を調べて、current_user に有効なユーザー (パスワード付き) が割り当てられているかどうかを確認します。

于 2012-08-05T05:20:08.937 に答える