Ruby on rails ActionController::UrlGenerationError在清单中#新建
我正在尝试为供应商和库存创建一个数据库网站。 每次我尝试创建“新库存”时,都会出现以下错误:Ruby on rails ActionController::UrlGenerationError在清单中#新建,ruby-on-rails,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:
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 %>
按钮“新库存”所在的位置
<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...
库存
“获取”%>
#更多代码。。。
此部分表单用于([@inventory,@inventory.photos.build])
表示它是一个用于照片的表单,照片属于@inventory
。此@inventory
应保存在您的数据库中。表单提交url可能是/inventory/1/photos
,但由于尚未创建@inventory
,因此会引发您看到的错误
从上下文来看,我认为您需要@inventory的
表单,这意味着库存的表单。若要在一个清单中包含多张照片,您可以使用字段\u进行提交表单时会发生这种情况吗?不会,当我单击“新建清单”按钮时,我会收到错误消息:您在哪里有新的清单?用相关代码更新问题。
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
<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...