I’m learning with the Agile Web D. 4th edition book. I’ve
built a method in my controller to test that it works, and now I want to
move it to the model.
*** line_item controller ***
def create
@cart = current_cart
event = Event.find(params[:event_id])
@line_item = @cart.add_event(event)
new_capacity = event.capacity
respond_to do |format|
if @line_item.save
new_capacity = new_capacity - @line_item.quantity
event.capacity = new_capacity
event.save
format.html { redirect_to(@line_item.cart, :notice => 'Line item
was successfully created.’) }
format.xml { render :xml => @line_item, :status => :created,
:location => @line_item }
else
format.html { render :action => “new” }
format.xml { render :xml => @line_item.errors, :status =>
:unprocessable_entity }
end
end
end
so I started by doing this
line_item.rb***********
def decrease_seat_capacity_on_create
new_capacity = new_capacity - @line_item.quantity
event.capacity = new_capacity
event.save
end
and made a changed the line_item controller like this
respond_to do |format|
if @line_item.save
@line_item.decrease_seat_capacity_on_create
But I get the error
undefined method `quantity’ for nil:NilClass
Rails.root: c:/webtech/ServerSide/tickets_online
Application Trace | Framework Trace | Full Trace
app/models/line_item.rb:12:in decrease_seat_capacity_on_create' app/controllers/line_items_controller.rb:51:in
block in create’
app/controllers/line_items_controller.rb:49:in `create’
So I’m a bit confused about ‘quantity’. I’ve used it in another method
in the line_item.rb and it worked (much to my surprise) but it doesn’t
work in this new method I’m writing.
I really am an absolute beginner so I’m really just guessing most of the
time. Any suggestions or hints would be most appreciated.
thanks in advance for your patience.