"stocks", :stockpile_id=>#, :id=>-6ren">
gpt4 book ai didi

ruby-on-rails - rails 4 : No route matches - issue with relationships missing id key

转载 作者:数据小太阳 更新时间:2023-10-29 07:23:51 25 4
gpt4 key购买 nike

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

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

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

这是我的 Controller :

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

这里是有问题的 View :

<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 的错误。

谢谢。

最佳答案

看起来你需要传入 stockpile 以及 stock..

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

应该这样做。

与使用作用域相反,您可以在路由中使用嵌套资源

  resources :stockpiles  do
resources :stocks
end

然后你可以干掉你的库存 Controller

  class StocksController < ApplicationController
before_action :set_stockpile

.....
def set_stockpile
@stockpile = Stockpile.find params[:stockpile_id]
end

关于ruby-on-rails - rails 4 : No route matches - issue with relationships missing id key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18389339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com