我希望有人能幫助我。我得到了以下問題:Rails 4:沒有路由匹配 - 問題與關係丟失ID密鑰
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]
當訪問以下網址:/管理/庫存/ 3 /股票/
我的路線是這樣的:
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錯誤。
謝謝。
當你改變'會發生什麼<%= link_to'顯示',股票%>'到'<%= link_to'顯示',stock.id%>'?或者,對於路線助手,更具規範性,'<%= link_to'顯示',stock_path(股票)%>'。 –
下一次嘗試'耙路線' – j03w
耙路線顯示stocks_path等,因爲它在一個範圍內(這是我想要的,因爲當從範圍中刪除並不想要重命名所有的路線)最後只是使用嵌套的資源。 – user2708672