model / songs.rb
require 'file_size_validator'
class Song < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
validates :attachment, :presence => true,
:file_size => {:maximum => 2.megabytes.to_i}
end
new.html.erb
<div class="well">
<%= form_for @song, html: { multipart: true } do |f| %>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<%= f.submit "Upload", class: "btn btn-primary" %>
<% end %>
</div>
当我提交没有文件选择的空表单或表单时,它会给出错误: -
ActionController::ParameterMissing at /songs
param is missing or the value is empty: songs
actionpack (4.1.4) lib/action_controller/metal/strong_parameters.rb
182 def require(key)
183 self[key].presence || raise(ParameterMissing.new(key))
184 end
我想知道为什么 ActiveRecords :: Base验证:presence =&gt;真的不能正常工作 (新到里亚尔)
songs_controller.rb
require 'zip'
require 'taglib'
class SongsController < ApplicationController
respond_to :html, :js
def index
session[:ids] = nil
@songs = Song.where("song_name IS NOT NULL AND song_path IS NOT NULL").page(1).per(5)
end
def new
@song = Song.new
@songs = Song.find(session[:ids]) if !session[:ids].nil?
end
def create
@song = Song.new(song_params)
if @song.save
puts @song.errors.messages
zip_file_path = Pathname.new(@song.attachment.current_path)
destination_path = zip_file_path.dirname
unzip(zip_file_path, destination_path, true) if zip_file_path.extname == ".zip"
session[:ids] = Array.new
Dir.glob("#{destination_path}" + '/*') do |music_files|
if %w(.mp3 .wav .aac .wma .m4a).include? File.extname(music_files)
song_details = extract_music_metadata(music_files)
if !song_details.nil?
@song = Song.new(song_details)
session[:ids].push(@song.id) if @song.save
end
else
File.delete(music_files)
end
end
redirect_to new_song_url, notice: "The contents of zip files are listed below !!!"
else
render "new"
end
end
def edit
@song = Song.find(params[:id])
end
def update
@songs = Song.where("song_name IS NOT NULL AND song_path IS NOT NULL").page(1).per(5)
@song = Song.find(params[:id])
@song.update_attributes(song_params)
if !session[:ids].nil?
@songs = Song.find(session[:ids])
end
end
def delete
@song = Song.find(params[:id])
end
def destroy
@songs = Song.where("song_name IS NOT NULL AND song_path IS NOT NULL").page(1).per(5)
@song = Song.find(params[:id])
@song.destroy
File.delete(@song.song_path)
if !session[:ids].nil?
@songs = Song.find(session[:ids])
end
end
private
def song_params
params.require(:song).permit(:attachment, :title, :artist, :album, :year, :track,
:genre, :comment)
end
# extract contents of a zip file
def unzip(zip, unzip_dir, remove_after = false)
Zip::File.open(zip) do |zip_file|
zip_file.each do |f|
f_path=File.join(unzip_dir, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
a = zip_file.extract(f, f_path) unless File.exist?(f_path)
end
end
FileUtils.rm(zip) if remove_after
end
def extract_music_metadata(music_files)
TagLib::FileRef.open(music_files) do |fileref|
unless fileref.null?
tag = fileref.tag
song_details = {
song_name: File.basename(music_files),
song_path: music_files,
title: tag.title,
artist: tag.artist,
album: tag.album,
year: tag.year,
track: tag.track,
genre: tag.genre,
comment: tag.comment
}
end
end
end
end
控制台出错:
在2014-10-16 11:45:10 +0545开始为127.0.0.1发布“/ songs” 由SongsController处理#create as HTML 参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“oLgt6szcww1 / 4FIiP4dQk2IGSPqQb3l7roS0E1l7ovk =”,“commit”=&gt;“上传”} 在1ms完成400 Bad Request
ActionController :: ParameterMissing - 缺少param或值为空:song: