Rails5.1.3でWebアプリケーション制作の勉強をしています。顧客が新規に登録しcustomersテーブルにレコードが作成され保存に成功したときに
customerモデルと関連付けられたテーブルを新規に作成する方法が良くわからないので教えてください。
コントローラのcreateアクションは以下になっています。
customers_controller.rb def create @customer_form = Visitor::CustomerForm.new @customer_form.assign_attributes(params[:form]) if @customer_form.save flash.notice = '顧客情報を新規に登録しました。' redirect_to action: 'new' else flash.now.alert = '入力に誤りがあります。' render action: 'new' end end
関連付けられたPointsテーブルを作成する場合、if @customer_form.save の次の行に以下のように記述すればいいのでしょうか?
customers_controller.rb def create @customer_form = Visitor::CustomerForm.new @customer_form.assign_attributes(params[:form]) if @customer_form.save customer = Customer.find(params[:id]) point = customer.build_point point.save flash.notice = '顧客情報を新規に登録しました。' redirect_to action: 'new' else flash.now.alert = '入力に誤りがあります。' render action: 'new' end end
特にcustomer = Customer.find(params[:id])で登録されたcustomerが検索されるのかがわかりません。
どなたか教えていただけないでしょうか?宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。