我的一种表格有问题。
<%= form_for [@listing, @bike, @photo], html: { multipart: true } do |f| %>
<div class="custom-file col-sm-10 mt-4">
<%= f.file_field :attachment, class: "custom-file-input" %>
<label class="custom-file-label">Ajouter une photo</label>
</div>
<%= f.submit "Enregistrer", class: "btn btn-primary float-sm-right mt-4" %>
<% end %>
当我浏览一个新文件并单击提交按钮时,一切正常。但是,当我单击提交按钮而不浏览任何图片时,出现此错误:
ActionController :: ParameterMissing(参数丢失或值为空:照片):
该表格放在我的index.html.erb中。有我的控制器方法:
class PhotosController < ApplicationController
before_action :authenticate_user!
before_action :find_listing, :find_bike
def index
@photos = @bike.photos
@photo = Photo.new
end
def create
@photo = Photo.new(photo_params)
if @photo.save
flash[:success] = "Photo ajoutée avec succès"
redirect_to listing_bike_photos_path(listing_id: @listing, bike_id: @listing.bike)
else
flash[:error] = @photo.errors.values
redirect_to listing_bike_photos_path(listing_id: @listing, bike_id: @listing.bike)
end
end
private
def photo_params
params.require(:photo).permit(:id, :attachment, :legend).merge(bike_id: @bike.id)
end
def find_listing
@listing = Listing.find(params[:listing_id])
end
def find_bike
@bike = Bike.find(params[:bike_id])
end
end
在这里,我的路线:
resources :listings, except: [:show, :edit] do
resources :bikes, except: [:edit] do
resources :photos
end
end
我想我做错了。但我看不出问题出在哪里。有人可以帮我找到问题吗?提前谢谢。
编辑
这是我的照片模特:
class Photo < ApplicationRecord
belongs_to :bike, optional: true
has_attached_file :attachment, styles: { medium: "300x300#", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/
validates :attachment, presence: true
end
我的相关日志:
在2018-09-06 09:32:13 +0200开始发布127.0.0.1的POST“ / listings / 10 / bikes / 10 / photos” 由PhotosController#create处理为HTML 参数:{“ utf8” =>“✓”,“ authenticity_token” =>“ MV4dOYOFinOZsJjr + LlFRMIsDtQyPOxmlaV6PhEUZ1I + l5QS / bvwBRWc / EXLuw59F / B1Rgt6 / 5r79 + bCiq3reg6ist =,” er“”“ >“ 10”,“ bike_id” =>“ 10”} 用户负载(0.3毫秒)选择
users
。*从users
到users
。id
= 3 ORDER BYusers
。id
ASC LIMIT 1 列表负载(0.2毫秒)SELECTlistings
。* FROMlistings
在listings
。id
= 10 LIMIT 1 自行车负载(0.2毫秒)选择bikes
。*从bikes
到bikes
。id
= 10极限1 在4毫秒内完成了400个错误请求(ActiveRecord:0.8毫秒)ActionController :: ParameterMissing(参数丢失或值为空:照片):
app / controllers / photos_controller.rb:36:in
photo_params' app/controllers/photos_controller.rb:15:in
create' 在营救/布局中渲染/Users/virginie/.rvm/gems/ruby-2.3.4/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb 呈现/Users/virginie/.rvm/gems/ruby-2.3.4/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb 呈现的/Users/virginie/.rvm/gems/ruby-2.3.4/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb(4.5ms) 呈现/Users/virginie/.rvm/gems/ruby-2.3.4/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb 呈现的/Users/virginie/.rvm/gems/ruby-2.3.4/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb(2.1ms) 呈现/Users/virginie/.rvm/gems/ruby-2.3.4/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb 呈现的/Users/virginie/.rvm/gems/ruby-2.3.4/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb(1.3ms) 在营救/布局内(108.1ms)内渲染的/Users/virginie/.rvm/gems/ruby-2.3.4/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb >
这是我的表的架构:
create_table "photos", force: :cascade, options: "ENGINE=InnoDB DEFAULT
CHARSET=utf8" do |t|
t.integer "bike_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "attachment_file_name"
t.string "attachment_content_type"
t.integer "attachment_file_size"
t.datetime "attachment_updated_at"
t.string "legend"
end