2017-06-02 29 views
0

我正在尝试为供应商和库存创建数据库网站。 每次我试图创建一个 “新的库存”,我得到这个错误:库存中的ActionController :: UrlGenerationError#new

Showing /home/ubuntu/workspace/sale/app/views/photos/_form.html.erb where line #1 raised: 

No route matches {:action=>"index", :controller=>"photos", :inventory_id=>nil}, missing required keys: [:inventory_id] 

app/views/photos/_form.html.erb:1:in `_app_views_photos__form_html_erb__3563489644525801712_69877342878680' 
app/views/inventories/_form.html.erb:131:in `_app_views_inventories__form_html_erb__1568294121665732726_69877343884460' 
app/views/inventories/new.html.erb:12:in `_app_views_inventories_new_html_erb___931080756330630523_69877343979180' 

错误的位置:

<%= form_for([@inventory, @inventory.photos.build]) do |f| %> 
    <div class="form-group"> 
    <%= f.label :image %> 
    <%= f.file_field :image, class: 'form-control'%> 
    </div> 
    <p> 
     <%= f.submit 'Upload Photo' %> 
    </p> 
    <% end %> 

存货控制器:

class InventoriesController < ApplicationController 
    def show 
     @inventory = Inventory.find(params[:id]) 
    end 

    def index 
     @inventories = Inventory.search(params[:search]) 
     #@inventories = Inventory.all.paginate(page: params[:page], per_page: 10) 
     # @inventories = Inventory.all 
    end 

    def new 
     @inventory = Inventory.new 
    end 

    def create 
     @inventory = Inventory.new(inventory_params) 


     if @inventory.save 
      redirect_to @inventory 
     else 
      render 'new' 
     end 
    end 

    def edit 
     @inventory = Inventory.find(params[:id]) 
    end 

    def update 
     @inventory = Inventory.find(params[:id]) 
     if @inventory.update(inventory_params) 
      redirect_to @inventory 
     else 
      render 'edit' 
     end 
    end 

    def destroy 
     @inventory = Inventory.find(params[:id]) 
     @inventory.destroy 

     redirect_to inventories_path 
    end 
    end 

    private 
    def inventory_params 
     params.require(:inventory).permit(:product_name, :brand_name, :item_id, :upc_code, :color, :department, :size, :condition, :fabric_type, :shipping_weight, :sku, :asin, :quantity, :cost_price, :sell_price, :key_product_features, :product_description, :search_terms, :status, :listing_in_usa, :listing_in_canada, :listing_in_mexico) 

    end 

照片控制器:

class PhotosController < ApplicationController 

#Index action, photos gets listed in the order at which they were created 
def index 
    @photos = Photo.order('created_at') 
end 

#New action for creating a new photo 
def new 
    @photo = Photo.new 
end 

#Create action ensures that submitted photo gets created if it meets the requirements 
def create 
    @inventory = Inventory.find(params[:inventory_id]) 
    @photo = @inventory.photos.create(photo_params) 
    redirect_to inventory_path(@inventory) 
end 

def destroy 
     @inventory = Inventory.find(params[:inventory_id]) 
     @photo = @inventory.photos.find(params[:id]) 
     @photo.destroy 
     redirect_to inventory_path(@inventory) 
    end 

private 

def photo_params 
    params.require(:photo).permit(:title, :image) 
end 

end 

库存模型

class Inventory < ApplicationRecord 
    has_many :photos, dependent: :destroy 

    def self.search(search) 
    if search 
     Inventory.where("lower(search_terms) LIKE ? OR lower(product_name) LIKE ? OR upc_code LIKE ? OR status LIKE ? OR sku LIKE ?", "%#{search.downcase}%", "%#{search.downcase}%","%#{search}%", "%#{search.downcase}%", "%#{search.downcase}%") 
    else 
     Inventory.all 
    end 
    end 
end 

照片模式

class Photo < ApplicationRecord 
    belongs_to :inventory 
    has_attached_file :image 
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/ 
end 

photos.html.erb

<div class="media"> 
    <div class="media-left"> 
    <%= link_to image_tag(photo.image.url, class: 'media-object'), photo.image.url, target: '_blank' %> 
    </div> 
    <div class="media-body"> 
    <h4 class="media-heading"><%= photo.title %></h4> 
    </div> 
</div> 


<p> 
    <%= link_to 'Delete Photo', [photo.inventory, photo], 
           method: :delete, 
           data: { confirm: 'Are you sure?' } %> 
</p> 

我的路线文件

Rails.application.routes.draw do 
    devise_for :users 
    get 'sessions/new' 

    get 'vendors/index' 

    resources :vendors do 
    resources :fotos 
    end 

    resources :inventories do 
    resources :photos 
    end 

    root 'vendors#index' 

end 

位置,其中按钮, “新的库存”位于

<body> 
    <div class = "head1"> 
     <h1>Inventory</h1> 

     <div class = "image1" > 
      <img src= "http://dx.deucex.com/i/logo.png" > 
     </div> 
    </div> 

</body> 

<table> 
    <tr> 
     <% if user_signed_in? %> 
      <%= button_to "New Inventory", new_inventory_path, :method => "get" %> 
     <% end %> 
#more code... 
+0

这是否发生在提交表单? – Iceman

+0

不,当我点击按钮“新库存”时,我收到错误 – bockdavidson

+0

你有哪些新库存?用相关代码更新问题。 – Pavan

回答

0

这部分form_for([@inventory, @inventory.photos.build])表示这是一张照片的形式,照片属于@inventory。这个@inventory应该已经保存在你的数据库中。表单提交网址可能为/inventories/1/photos,但由于@inventory尚未创建,因此会引发您看到的错误。

从上下文来看,我认为你需要form_for @inventory这意味着一个形式的库存。要在广告资源中添加多张照片,您可以使用fields_for

相关问题