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 3 years have passed since last update.

DryDryDryな一日(if 文[条件分岐]〜update)

Last updated at Posted at 2021-04-21

コードレビュー中です。

    if @item.valid?
      @item.update(item_params)
修正ありがとうございます!
updateメソッドにはvalid?の機能も含まれているので、省略しましょう。
if文は「update出来たかどうか」で分岐するようにしましょう。

とコードレビューいただきました。
内容的には「Dry」のようです。
繰り返しはさけますが、
if内って実際に行われるれるんでしたっけ?

app/controllers/users_controller.rb
  def update
    if current_user.update(user_params)
      redirect_to root_path
    else
      render :edit
    end
  end

確かにif文だけに書かれていて次の処理文には出てきてませんね。

(修正前)

itemscontoroller.rb
 def update
    if @item.update(item_params)
       @item.update(item_params)
      render 'show'
    else
      render 'edit'
    end
  end

if分は中身が実際に実行されるので

単にtrue/falseが出されるだけではない。

(修正後)

itemscontoroller.rb
# 修正後
 def update
    if @item.update(item_params)
      render 'show'
    else
      render 'edit'
    end
  end
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?