title: Rails 4: No route matches - issue with relationships missing id key
body: <p>I am hoping someone can help me. I am getting the following issue:</p>
  
  <pre><code>No route matches {:action=&gt;"show", :controller=&gt;"stocks", :stockpile_id=&gt;#&lt;Stock id: 17, stockpile_id: 3, code: "rttrtrt", name: "", description: "", quantity: nil, cost_pence: nil, information: "", created_at: "2013-08-18 19:52:46", updated_at: "2013-08-18 19:52:46"&gt;, :id=&gt;nil, :format=&gt;nil} missing required keys: [:id]
  </code></pre>
  
  <p>When visiting the following URL: /admin/stockpiles/3/stocks/</p>
  
  <p>My routes look like:</p>
  
  <pre><code>scope '/admin' do
    root :to =&gt; 'admin#index', :as =&gt; 'admin'
    resources :stockpiles,:companies
  
    scope :path =&gt; 'stockpiles/:stockpile_id' do
      resources :stocks
    end
  end
  </code></pre>
  
  <p>The data contained in the error message:</p>
  
  <pre><code>id | stockpile_id |   code   | name | description | quantity | cost_pence | information |         created_at         |         updated_at         
  17 |            3 | rttrtrt  |      |             |          |            |             | 2013-08-18 19:52:46.856864 | 2013-08-18 19:52:46.856864
  </code></pre>
  
  <p>My Stock Model:</p>
  
  <pre><code>class Stock &lt; ActiveRecord::Base
    belongs_to :stockpile
  end
  </code></pre>
  
  <p>Nothing interesting in the Stockpile model, just that it has many stocks..</p>
  
  <p>Here is my controller:</p>
  
  <pre><code>class StocksController &lt; ApplicationController
    before_action :set_stock, only: [:show, :edit, :update, :destroy]
  
    def index
      @stockpile = Stockpile.find(params[:stockpile_id]) 
      @stocks = @stockpile.stocks.all 
    end
  
    def show
    end
  
    def new
      @stockpile = Stockpile.find(params[:stockpile_id])
      @stock = @stockpile.stocks.new
    end
  
    def edit
    end
  
    def create
      @stockpile = Stockpile.find(params[:stockpile_id])
      @stock = @stockpile.stocks.new(stock_params)
  
      if @stock.save
        redirect_to @stock, notice: 'Stock was successfully created.'
      else
        render action: 'new'
      end
    end
  
    def update
      if @stock.update(stock_params)
        redirect_to @stock, notice: 'Stock was successfully updated.'
      else
        render action: 'edit'
      end
    end
  
    def destroy
      @stock.destroy
      redirect_to stocks_url, notice: 'Stock was successfully destroyed.'
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_stock
        @stockpile = Stockpile.find(params[:stockpile_id])
        @stock = @stockpile.stocks.find(params[:id])
      end
  
      # Only allow a trusted parameter "white list" through.
      def stock_params
        params.require(:stock).permit(:code, :name, :description, :quantity, :cost_pence, :information)
      end
  end
  </code></pre>
  
  <p>And here is the view in question:</p>
  
  <pre><code>&lt;div class="page-header"&gt;
    &lt;%= link_to new_stock_path(params[:stockpile_id]), :class =&gt; 'btn btn-primary' do %&gt;
      &lt;i class="icon-plus icon-white"&gt;&lt;/i&gt;
      New Stock
    &lt;% end %&gt;
    &lt;h1&gt;Listing stocks&lt;/h1&gt;
  &lt;/div&gt;
  
  &lt;table class="table table-bordered table-striped"&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Stockpile&lt;/th&gt;
        &lt;th&gt;Code&lt;/th&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Description&lt;/th&gt;
        &lt;th&gt;Quantity&lt;/th&gt;
        &lt;th&gt;Cost pence&lt;/th&gt;
        &lt;th&gt;Information&lt;/th&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
  
    &lt;tbody&gt;
  &lt;% @stocks.each do |stock| %&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;%= stock.stockpile %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= stock.code %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= stock.name %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= stock.description %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= stock.quantity %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= stock.cost_pence %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= stock.information %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= link_to 'Show', stock %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= link_to 'Edit', edit_stock_path(stock) %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= link_to 'Destroy', stock, confirm: 'Are you sure?', method: :delete %&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;% end %&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Help would be much appreciated - looks like something weird is going on because stockpile_id seems to be set to stock, and there seems to be no stock params or anything - so we get the missing id error.</p>
  
  <p>Thanks.</p>