I am a newbie to rails. Please excuse this.
I am trying to implement 'after_create' into my controller, in order to make data into different table in DB.
after_create :create_order_confirm
It will call this.
def create_order_confirm
my_order_confirm = OrderConfirm.new(:order_id => @order.id.to_s)
my_order_confirm.save
end
but, every time I try to render 'new', I got error following.
These are my full controller. (excluded not relevant part..)
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
after_create :create_order_confirm
# GET /orders
# GET /orders.json
def index
@orders = Order.all
end
# GET /orders/1
# GET /orders/1.json
def show
render "show"
end
# GET /orders/new
def new # Error -> happen.
@order = Order.new
end
...
...
private
# Use callbacks to share common setup or constraints between actions.
def set_order
@order = Order.find(params[:id])
end
def create_order_confirm
my_order_confirm = OrderConfirm.new(:order_id => @order.id.to_s)
my_order_confirm.save
end
end
It seems my new method wasn't called.
@order = Order.new
If I remove the 'after_create :create_order_confirm' this line. It's working fine. Would anyone explain this? and How can I use 'after_create'? I only want them to be called if the 'create' function has been called.
EDIT
changing order to @order didn't solve the problem.
after_create is ActiveRecord callback. Not available in controller.