ruby-on-rails – Rails 4:没有路由匹配 – 缺少id密钥的关系问
发布时间:2020-12-17 03:55:25 所属栏目:百科 来源:网络整理
导读:我希望有人可以帮助我.我收到以下问题: 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: "201
我希望有人可以帮助我.我收到以下问题:
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] 访问以下网址时:/ 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模型没有什么有趣的,只是它有很多股票.. 这是我的控制器: 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错误. 谢谢. 解决方法
看来你需要传递库存和库存..
<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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |