没有路由匹配{:action =>" show",:controller =>" items"}缺少必需的密钥:[:id]

时间:2015-11-16 20:06:23

标签: ruby-on-rails

  

更新:我一直在尝试调试我的文件,因此大部分文件最近都发生了变化

我在尝试使用" new"时遇到一个奇怪的错误。行动到我的items_controller。基本上是wishlist has_many itemsitem belongs_to wishlist。错误消息如下:

UrlGenerationError

代码

这是我的items_controller

class ItemsController < ApplicationController
  def show
    @item = Item.find(params[:id])
  end
  def new
    @item = Item.new
  end
  def create
   @item = Item.new(item_params)
    if @item.save
      redirect_to "/wishlist", :notice => "Success!"
    else
      redirect_to "/wishlist", :notice => "Failure, try again later!"
    end
  end
  def edit
    @item = Item.find(params[:id])
  end
  def update
    @item = Item.find(params[:id])
    if @item.update_attributes(item_params)
      redirect_to(:action => 'show', :id => @item.id)
    else
      render 'edit'
    end
  end
  private
  def item_params
    params.require(:item).permit(:name,:size,:qty,:priority)
  end
  private
  def create_params
    params.require(:item).permit(:name,:size,:qty,:priority,:wishlist_id)
  end
end

我的routes.rb

Rails.application.routes.draw do
  get '/wishlist' => 'wishlists#index', as: :wishlists
  get '/wishlist/:id' => 'wishlists#show', as: :wishlist
  get '/wishlist_items/new' => 'items#new'
  get '/wishlist_items/:id' => 'items#show', as: :items
  get '/wishlist_items/:id/edit' => 'items#edit', as: :edit_items
  patch '/wishlist_items/:id' => 'items#update'
  resources :items

最后,我new.html.erb模型的items

<h1>New Item</h1>
<div class="wishlist-new">
    <% if true %>
    <%= form_for(@item) do |f| %>
        <%= f.text_field :name, :placeholder => "Name" %>
        <%= f.text_field :size, :placeholder => "Specifications" %>
        <%= f.text_field :qty, :placeholder => "Quantity" %>
        <%= f.text_field :priority, :placeholder => "Priority" %>
        <%= f.text_field :id, :placeholder => "Wishlist ID" %> # changing :id to :wishlist_id doesn't seem to do anything
        <%= f.submit "Create Item", class: "btn-submit" %>
    <% end %>
    <% end %>
</div>

我的迁移文件(因此您知道我的数据库是如何构建的:

# Migration file for items
class CreateItems < ActiveRecord::Migration
  def change
    drop_table :items
    create_table :items do |t|
      t.string :name
      t.string :size
      t.string :qty
      t.integer :priority
      t.references :wishlist
      t.timestamps
    end
  end
end

# Migration File for Wishlists
class CreateWishlists < ActiveRecord::Migration
  def change
    drop_table :wishlists
    create_table :wishlists do |t|
      t.string :title
      t.timestamps
    end
  end 
end  

尝试调试

似乎routes.rb正在向items_controller中的不同方法发送请求,因为错误似乎表明/wishlist_items/new正在访问show方法,即使它{ {1}}方法优先。为了支持这一点,当我在路由文件中注释new时,页面正确加载。发生的事情是页面正确加载,并且正确创建了项目(当我填写表单时)除了当我进入控制台时,它说创建的项目具有{{{ 1}}即使我在表单中指定它为get '/wishlist_items/:id' => 'items#show', as: :items

上述方法存在两个问题:(1)它没有完全正确地工作,(2)无法在心愿单中wishlist_id: nil特定项目。

  

在加载1的内部部分之前发生错误,所以问题要么是(a)奇怪的路由事物(如上所述)或(b)奇怪的事情发生在show变量。

提前致谢!

0 个答案:

没有答案