0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Posted at

プロフィールを作成・更新

app/controllers/profiles_controller.rb
  def update
    @profile = current_user.build_profile(profile_params)
    if @profile.save
      redirect_to profile_path, notice: 'プロフィール更新!'
    else
      flash.now[:error] = '更新できませんでした'
      render :edit
    end
  end

  private
  def profile_params
    params.require(:profile).permit(
      :nickname,
      :introduction,
      :gender,
      :birthday,
      :subscribed
    )
  end

実際にサイトで、プロフィールを保存後
rails コンソールにて

$User.first.profile

とやると

Profile id: 1, user_id: 1, nickname: "くま🐢", introduction: "プログラミング学習中!\r\nよろしくおねがしまーす!!", gender: "male", birthday: "1999-12-20", subscribed: false, created_at: "2023-03-14 09:00:45", updated_at: "2023-03-14 09:00:45"
と表示される👍

保存できていますね😆

性別の部分が、maleとなっています!
DBでは、0が保存されているが、アクティブレコードが気をきかせて0だからmaleだよね!と勝手に訳してくれている🙆

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?