LoginSignup
38
42

More than 5 years have passed since last update.

mergeメソッドで、入力してもらったparamsと一緒に予め別のテーブルに持っている情報を保存する

Posted at

def create
Question.create(create_params)
redirect_to :root and return
end

private
def create_params
params.require(:question).permit(:text).merge(user_id: current_user.id, group_id: current_user.group_id)
end

mergeメソッドを使うことによって、ストロングパラメーター(create_params)が生成される際にuser_idとgroup_idのキーと値を持つハッシュを追加することができます。

38
42
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
38
42