title: undefined method 'book_id' for nil:NilClass
body: <p>I'm trying to make a simple book ordering application in Ruby on Rails and have been running into some problems. I am trying to create a "place an order" page from a link from each product, so that on this second page the book information is there and then there's just a form to fill in the quantity the customer wants. However I keep running into the undefined method 'book_id' for nil:NilClass error specified above (and when I delete that line of code I run into the same error on the next one - i.e. undefined method 'order_id').</p>
  
  <p>Here's my code for the view that I keep messing up on: </p>
  
  <pre><code>&lt;%= @order_item.book_id %&gt;
  &lt;%= @order_item.order_id %&gt;
  &lt;%= @order_item.total_pricel %&gt;
  &lt;%= form_for @order_item.quantity do |f| %&gt;
  &lt;%= f.label :quantity %&gt;:
  &lt;%= f.text_field :quantity %&gt;&lt;br /&gt;
   &lt;%= f.submit %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>My OrderItem model should have the foreign keys order_id and book_id.</p>
  
  <p>Here's my OrderItem model:</p>
  
  <pre><code>class OrderItem &lt; ActiveRecord::Base
  belongs_to :order
  belongs_to :book
  
  validates :quantity, presence: true, numericality: {only_integer: true, greater_than: 0}
  validate :book_present
  validates :price, presence: true
  validate :order_present
  
  def unit_price
      if persisted?
          self[:unit_price]
      else
          book.price
      end
  end
  
  def total_price
      unit_price * quantity
  end
  
  private
      def book_present
          if book.nil?
              errors.add(:book, "is not valid.")
          end
      end
  
  
  def order_present
      if order.nil?
          errors.add(:order, "is not a valid order.")
          end
      end
  
  def finalize
      self[:unit_price] = unit_price
      self[:total_price] = quantity * self[:unit_price]
  end
  end
  </code></pre>
  
  <p>And here's the controller: </p>
  
  <pre><code>class OrderItemsController &lt; ApplicationController
  
  def create
      @order = current_order
      @order_item = @order.order_items.new(order_item_params)
      @order.save
      session[:order_id] = @order.id
  end
  
  def update
      @order = current_order
      @order_item = @order.order_items.find(params[:id])
      @order_item.update_attributes(order_item_params)
      @order_items = @order.order_items
  end
  
  def destroy
      @order = current_order
      @order_item = @order.order_items.find(params[:id])
      @order_item.destroy
      @order_item = @order.order_items
  end
  
  private
  def order_item_params
      params.require(:order_item).permit(:quantity, :book_id)
  end
  end
  </code></pre>
  
  <p>In addition to this being my first Rails project, I'm generally very new to coding and would really appreciate some help!</p>