4

我希望有人能帮助我。我收到以下问题:

No route matches {:action=>"show", :controller=>"stocks", :stockpile_id=>#<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">, :id=>nil, :format=>nil} missing required keys: [:id]

访问以下 URL 时:/admin/stockpiles/3/stocks/

我的路线看起来像:

scope '/admin' do
  root :to => 'admin#index', :as => 'admin'
  resources :stockpiles,:companies

  scope :path => 'stockpiles/:stockpile_id' do
    resources :stocks
  end
end

错误消息中包含的数据:

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

我的库存型号:

class Stock < ActiveRecord::Base
  belongs_to :stockpile
end

库存模型没有什么有趣的,只是它有很多库存。

这是我的控制器:

class StocksController < 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

这是有问题的观点:

<div class="page-header">
  <%= link_to new_stock_path(params[:stockpile_id]), :class => 'btn btn-primary' do %>
    <i class="icon-plus icon-white"></i>
    New Stock
  <% end %>
  <h1>Listing stocks</h1>
</div>

<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>Stockpile</th>
      <th>Code</th>
      <th>Name</th>
      <th>Description</th>
      <th>Quantity</th>
      <th>Cost pence</th>
      <th>Information</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>

  <tbody>
<% @stocks.each do |stock| %>
    <tr>
      <td><%= stock.stockpile %></td>
      <td><%= stock.code %></td>
      <td><%= stock.name %></td>
      <td><%= stock.description %></td>
      <td><%= stock.quantity %></td>
      <td><%= stock.cost_pence %></td>
      <td><%= stock.information %></td>
      <td><%= link_to 'Show', stock %></td>
      <td><%= link_to 'Edit', edit_stock_path(stock) %></td>
      <td><%= link_to 'Destroy', stock, confirm: 'Are you sure?', method: :delete %></td>
    </tr>
<% end %>
  </tbody>
</table>

非常感谢您的帮助-看起来有些奇怪,因为 stockpile_id 似乎设置为库存,并且似乎没有库存参数或任何东西-因此我们收到了缺少 id 错误。

谢谢。

4

1 回答 1

10

看来您需要传递库存以及库存..

 <td><%= link_to 'Show', [@stockpile, stock] %></td>

应该这样做。

与使用范围相反,您可能只在路由中使用嵌套资源

  resources :stockpiles  do
      resources :stocks 
  end 

然后你可以干掉你的库存控制器

  class StocksController < ApplicationController
    before_action :set_stockpile 

    .....
    def set_stockpile 
      @stockpile = Stockpile.find params[:stockpile_id]
    end 
于 2013-08-24T20:37:08.257 回答