存货中的ActionController :: UrlGenerationError #new

时间:2017-06-02 15:31:14

标签: ruby-on-rails

我正在尝试为供应商和库存创建数据库网站。 每当我尝试创建一个"新库存"时,我都会收到此错误:

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

按钮的位置,&#34;新库存&#34;位于

<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...

1 个答案:

答案 0 :(得分:0)

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

从上下文来看,我认为你想要form_for @inventory这意味着一个库存表单。要在广告资源中放置多张照片,您可以使用fields_for