LoginSignup
0
0

More than 1 year has passed since last update.

ユーザのプロフィールページ作成⑤

Posted at

プロフィール編集

現状では、プロフィールは保存できているが、editで見ると、何も表示されません!

現在editでは、空のインスタンスを渡している。

  def edit
    if current_user.profile.present?
      @profile = current_user.profile
    else
      @profile = current_user.build_profile
    end

こうすることで、プロフィールに値が存在していれば表示する。なければ、空のインスタンスを入れる。

@profile = current_user.profile || current_user.build_profile

また、この書き方でも、同じ意味となり、簡単に表すことできる。

app/models/user.rb
よく使う表現なので、ここに保存しておきます!

  def prepare_profile
    profile || build_profile
  end

app/controllers/profiles_controller.rb

@profile = current_user.prepare_profile

こうします👍

さらにアップデート部分を更新しても対応するように変更!

@profile = current_user.prepare_profile
@profile.assign_attributes(profile_params)

内容変更後、rails cで

$Profile.first

で見ると変更されているのが確認できます👍

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0