On Sun, May 8, 2011 at 12:00 PM, linda Keating <li...@ruby-forum.com> wrote:
> I'm learning with the Agile Web Development 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 > In this method @line_item does not exist because you have not created it. So you may need to pass to this method the line_item object or look it up. Also, you should look at the after_save callback in ActiveRecord, that way you *might* be able to tell your LineItem model to automatically perform this action after a successful save (if you google for activerecord callbacks you will find a number of them, they are very useful). > > 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. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.